25

How slice string in java? I'm getting row's from csv, and xls, and there for example data in cell is like

14.015_AUDI

How can i say java that it must look only on part before _ ? So after manipulating i must have 14.015. In rails i'll do this with gsub, but how do this in java?

1
  • Just to note, using gsub for this in Rails is massive overkill, you should use split, same as Java. Commented Oct 22, 2012 at 21:38

6 Answers 6

28

You can use String#split:

String s = "14.015_AUDI";
String[] parts = s.split("_"); //returns an array with the 2 parts
String firstPart = parts[0]; //14.015

You should add error checking (that the size of the array is as expected for example)

Sign up to request clarification or add additional context in comments.

3 Comments

Could you maybe add a method for actually slicing strings? Or maybe OP could change post title?
If you truly insist on the split method for this, use s.split("_", 2) to waste less work -- but a perhaps more conceptually correct solution is the traditional indexOf+ substring.
Typical a "slice" is something, which shares storage. split returns two newly allocated strings.
22

Instead of split that creates a new list and has two times copy, I would use substring which works on the original string and does not create new strings

String s = "14.015_AUDI";
String firstPart = s.substring(0, s.indexOf("_"));

5 Comments

indexof must be changed in indexOf
@Youssef, Thanks fixed
This is wrong, it returns AUDI. Needs to be String firstPart = s.substring(0, s.indexOf("_")+1); OP wanted 14.015
Thanks, I fixed it
At least in Java 8, substring creates new strings, see in String.java line 2709ff: return isLatin1() ? StringLatin1.newString(value, beginIndex, subLen) : StringUTF16.newString(value, beginIndex, subLen); In StringUTF16, line 1179, then for example: return new String(Arrays.copyOfRange(val, index << 1, last << 1), UTF16);
4
String str = "14.015_AUDI";
String [] parts = str.split("_");

String numberPart = parts[0];
String audi = parts[1];

Comments

2

Should be shorter:

"14.015_AUDI".split("_")[0];

5 Comments

You do not use any error handling here. Bad. In this case it can be acceptable because the split() of an empty string will yield an array with a single empty string, but if you wanted to access element [1], you would get an exception. Always check possible errors!!
This is just an example of operation. If you need more functionality - you have to write your own program.
Shorter than what? If you love the split method for this, you should pass a limit like .split("_", 2) to save memory and time.
This is outside of the question. The first rule of optimization - do not optimize. If it is needed - that's another question.
On this way we can detect JavaScript developer – all should be shorter! ;-) I think it is better because the Java class file is small. Поздравляю с 10000! ;-)
1

you can use substring!

"substring(int begIndex, int endIndex)"

eg:

String name  = "14.015_AUDI";
System.out.println(name.substring(0,6));

Comments

0

Guava has Splitter

List<String> pieces = Splitter.on("_").splitToList("14.015_AUDI");
String numberPart = parts.get(0);
String audi = parts.get(1);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.