Java String , trick in split - java

I have a string with , separated I want to read till 4th index then remaining string I want to consider as one string.
Like in below
String str = "abc,xyz,123,789,ijk,1232,123,123,STU,PQR,111";
I want to split and take string after ijk in one string and from abc to 789 each part in different string.

String::split can take a second parameter indicating how many groups to form, which in your case is 5:
String[] result = str.split(",", 5);

Related

Splitting String in Java with empty elements

I'm reading from a .csv File line by line. One line could look for example as following: String str = "10,1,,,,".
Now I would like to split according to ",": String[] splitted = str.split(","); The problem now is that this only results in 2 elements but I would like to have 5 elements, the first two elements should contain 10 and 1 and the other 3 should be just an empty String.
Another example is String str = "0,,,,," which results in only one element but I would like to have 5 elements.
The last example is String str = "9,,,1,," which gives 2 elements (9 and 1), but I would like to have 5 elements. The first element should be 9 and the fourth element should be 1 and all other should be an empty String.
How can this be done?
You need to use it with -1 parameter
String[] splitted = str.split(",", -1);
This has been discussed before, e.g.
Java: String split(): I want it to include the empty strings at the end
But split really shouldn't be the way you parse a csv, you could run into problems when you have a String value containing a comma
23,"test,test","123.88"
split would split the row into 4 parts:
[23, "test, test", "123.88"]
and I don't think you want that.
split only drops trailing delimeters by default. You can turn this off with
String str = "9,,,1,,";
String[] parts = str.split(",", -1);
System.out.println(Arrays.toString(parts));
prints
[9, , , 1, , ]
Pass -1 (or any negative number, actually) as a second parameter to split:
System.out.println("0,,,,,".split(",", -1).length); // Prints 6.

How to unformat a formatted String

Hi I have formatted a string using the below code
mForecastStr = String.format("%s - %s - %s/%s",dateView.getText(),
forecastView.getText(), highView.getText(),
lowView.getText());
Now I want to separate the values from mForecastStr
Like the value before the first hyphen, then the second hyphen and the n the value after the third hyphen.
How can I do that?
To get an array of string with the split values:
String[] arrForecast = mForecastStr.split(" - ");
Now arrForecast[0] will contain the first value, arrForecast[1] the second one and arrForecast[2] the third one.

Java: single line substring

I need to sub string the string after "2:" to the end of line as it is a changeable string:
Which mean in this example that I want to take the string "LOV_TYPE" from this 2 lines
ObjMgrSqlLog Detail 4 2014-03-26 13:19:58 Bind variable 2: LOV_TYPE
ObjMgrSqlLog Detail 4 2014-03-26 13:19:58 Bind variable 3: AUDIT_LEVEL
I tried to use subString(int startingIndex, int endingIndex) method, I can determine the first argument which is starting point.. but I can't determine the end point.
You can use two substrings, one that gets the String after 2:, and then one that gets the string before the next new line.
string = string.substring(string.indexOf("2:") + 2);
string = string.substring(0, string.indexOf("ObjMgrSqlLog));
If you need to get rid of the spaces on either end, you can then trim the string.
string = string.trim();
source:
String str = "ObjMgrSqlLog Detail 4 2014-03-26 13:19:58 Bind variable 2: LOV_TYPE";
You can use regex
String out1 = str.replaceAll("^.*?.\\:.*[ ]", "");
or classic index-of
int lastCh = str.lastIndexOf(":");
String out2 = str.substring(++lastCh).trim();
output:
System.out.println(out1);
System.out.println(out2);
If you use str.substring(startingIndex), you will have the substring to the end of the string. It seems to be what you want. If you have extra spaces at the end of the string, you can always use a str.trim() to remove the spaces.
Use substring along with .length() to get the value of the length of the string. For example:
String original = "ObjMgrSqlLog Detail 4 2014-03-26 13:19:58 Bind variable 2: LOV_TYPE";
String newString = original.substring (62, original.length ());
System.out.print (newString);

Only showing part of string

This may sound a bit strange but I'm trying to only show part of a string that is retrieved. The string that is retrieved contains something only the lines of NAME:myname and I'm trying to only show the "myname" part is there a way to 'disect' a string considering I know what the prefix "NAME:" is all ways going to be?
There are plenty of ways:
Replace "NAME:" by nothing.
String cleaned = myString.replace("NAME:", "");
Split the string (as shown in the other answer).
Cut the string (if it always starts with NAME: which length is 5):
String cleaned = myString.subString(5);
Use a regular expression
Probably 200 other ways.
Yes. Use something like:
String arr[] = myString.Split(":");
String name = arr[1];
arr[] will contain 2 elements (0 and 1).
arr[0] will contain "Name"
and
arr[1] will contain the second part (the name itself)
Another version of the same (1 line only):
String name = myString.Split(":")[1];
Use split method :
String name = tmpStr.split(":")[tmpStr.split(":").length-1] ;

Cutting / splitting strings with Java

I have a string as follows:
2012/02/01,13:27:20,872226816,-1174749184,2136678400,2138578944,-17809408,2147352576
I want to extract the number: 872226816, so in this case I assume after the second comma start reading the data and then the following comma end the reading of data.
Example output:
872226816
s = "2012/02/01,13:27:20,872226816,-1174749184,2136678400,2138578944,-17809408,2147352576";
s.split(",")[2];
Javadoc for String.split()
If the number you want will always be after the 2nd comma, you can do something like so:
String str = "2012/02/01,13:27:20,872226816,-1174749184,2136678400,2138578944,-17809408,2147352576";
String[] line = str.split(",");
System.out.println(line[2]);

Categories

Resources