tags:

views:

180

answers:

8

For example I have a string "1234" that I want to break into two strings "12", "34". Is there any method for doing that? How?

+10  A: 

You might use the substring function:

"1234".substring(0, 2);
"1234".substring(2, 4);

It takes a begin index and an end index, returning the substring between the two.

luvieere
While it's really nice to jump on questions like this in order to boost your rep here, you really shouldn't provide answers to people who haven't even tried to find them out for themselves first. One of these days, you and I are maybe going to have to maintain or support the "code" they've produced. See @delfuego and @camickr's comments to the original question.
Ken White
@ken I've learned some things the same way, by asking and being answered to the point. It terribly irritates me when I ask something and I'm being answered with a general "look for it yourself" kind of reply - I generally ask about two types of things: the ones that I'm really interested in but I couldn't find an answer even after extensive searching and the ones that I wouldn't give a f*ck about even if you would wire me to a f*ck giving machine - but I have to do the thing anyway, so I seek community help. This is why I understand such questions and try to be helpful to the point.
luvieere
Luvieere, I find it hard to imagine that someone of your ilk and rep here would neglect to even *open the damn Javadocs* before posting your question to SO -- that's the distinction we're trying to make. Look at Johanna's posting history, and convince me/us that she's actually making **any** effort other than clicking a bookmark to the SO "Post a new question" page, typing a semi-comprehensible string of jibberish into the box, and clicking submit...
delfuego
I won't try to convince you of anything **but** of the fact that I would do **the very same thing** if it were about a math problem. I **hate** math, I truly do, I used to cheat at math papers in high school so I could get a D - so I would make **the same** amount of infinitesimal effort if it were for me to solve a math problem at work, and I would blatantly ask the damn thing on a forum or something. I personally know people that have the same adversity for Java that I have for math - therefore I do not blame them.
luvieere
+1  A: 

You are probably looking for substring()

Lee
A: 

try looking at the javadocs, substring sounds useful ;-)

vickirk
+2  A: 

If you want to generically solve this problem (let's say you want to split a string in half), then extend what luvieere suggested...

String aString = "1234";
int length = aString.length();
System.out.println(aString.substring(0,length / 2));
System.out.println(aString.substring(length / 2,aString.size());

Something like that. (Of course, that doesn't handle an odd-length string, but I'll leave that up to you.)

JasCav
A: 

Whenever you have to do something with Strings, then have a look at the static methods declared on the String class.

In fact String.substring() can't 'break' a String in two parts, but it returns a part of the original String and you can assemble your break requirement with two substring operations, like luvieere explained in another answer.

Usually you use String.split() to break a String in parts, but with substring there's an easier solution for your problem.

Andreas_D
"...look at the static methods..." substring() and split() aren't static.
Jared Russell
Oooops - thank you :-))
Andreas_D
A: 

use the string split method

for example

  String s = "1234";
  s.Split(".{2}.{2}" = {"12", 23"}  // a String array
Alon
- compilation errors: method split must start with lowercase,... - logic error: results in empty string, the regex `.{2}.{2}` is the delimiter, not what is being searched for.
Carlos Heuberger
A: 

You could also you String Tokenizer. If there is a character that you want to split the string with.

For example "Stack Overflow", could be split into two tokens "Stack" and "Overflow" using the white-space (" ").

JavaDoc

Examples

Berek Bryan
+1  A: 

If your question is actually "what separator should I use to demarcate individual values in a string", the answer is it doesn't matter as long as it's not going to be present in your normal input data. For example, you could use "^|*" as your separator in most situations because it's unlikely to be coincidentally present in normal input data.

If your question is "how do I split a string delimited by a known separator", you can use StringTokenizer, String.split() with a regex, or String.indexOf() combined with String.substring().

spork