PavelBY PavelBY - 4 years ago 262
Java Question

Slice string in java

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


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?

Answer Source

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)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download