Java Split - wrong length - java

Why am I receiving a length of 3 instead of 4? How can I fix this to give the proper length?
String s="+9851452;;FERRARI;;";
String split[]=s.split("[;]");
System.out.println(split.length);

You're receiving a length of 3 because for split,
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
If you specify a negative limit, it'll work fine:
String s="+9851452;;FERRARI;;";
String split[]=s.split(";", -1);
System.out.println(Arrays.toString(split));
You'll just need to ignore or remove the 5th item, or remove the trailing ; - it shows up because there are 5 (potentially blank) strings on either sides of 4 tokens. See the docs for more info.

String split[]=s.split("[;]", -1);

The answer for WHY it's not working is in the doc: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#split%28java.lang.String%29 . "Trailing empty strings are therefore not included in the resulting array.".

You can use StringUtils from apache commons lang.
String s="+9851452;;FERRARI;;";
Arrays.toString(StringUtils.splitPreserveAllTokens(s, ";"))

Related

String.split() returns an array with an additional empty value

I'm working on a piece of code where I've to split a string into individual parts. The basic logic flow of my code is, the numbers below on the LHS, i.e 1, 2 and 3 are ids of an object. Once I split them, I'd use these ids, get the respective value and replace the ids in the below String with its respective values. The string that I have is as follow -
String str = "(1+2+3)>100";
I've used the following code for splitting the string -
String[] arraySplit = str.split("\\>|\\<|\\=");
String[] finalArray = arraySplit[0].split("\\(|\\)|\\+|\\-|\\*");
Now the arrays that I get are as such -
arraySplit = [(1+2+3), >100];
finalArray = [, 1, 2, 3];
So, after the string is split, I'd replace the string with the values, i.e the string would now be, (20+45+50)>100 where 20, 45 and 50 are the respective values. (this string would then be used in SpEL to evaluate the formula)
I'm almost there, just that I'm getting an empty element at the first position. Is there a way to not get the empty element in the second array, i.e finalArray? Doing some research on this, I'm guessing it is splitting the string (1+2+3) and taking an empty element as a part of the string.
If this is the thing, then is there any other method apart from String.split() that would give me the same result?
Edit -
Here, (1+2+3)>100 is just an example. The round braces are part of a formula, and the string could also be as ((1+2+3)*(5-2))>100.
Edit 2 -
After splitting this String and doing some code over it, I'm goind to use this string in SpEL. So if there's a better solution by directly using SpEL then also it would be great.
Also, currently I'm using the syntax of the formula as such - (1+2+3) * 4>100 but if there's a way out by changing the formula syntax a bit then that would also be helpful, e.g replacing the formula by - ({#1}+{#2}+{#3}) *
{#4}>100, in this case I'd get the variable using {# as the variable and get the numbers.
I hope this part is clear.
Edit 3 -
Just in case, SpEL is also there in my project although I don't have much idea on it, so if there's a better solution using SpEL then its more than welcome. The basic logic of the question is written at the starting of the question in bold.
If you take a look at the split(String regex, int limit)(emphasis is mine):
When there is a positive-width match at the beginning of this string then an empty leading substring is included at the beginning of the resulting array.
Thus, you can specify 0 as limit param:
If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.
If you keep things really simple, you may be able to get away with using a combination of regular expressions and string operations like split and replace.
However, it looks to me like you'd be better off writing a simple parser using ANTLR.
Take a look at Parsing an arithmetic expression and building a tree from it in Java and https://theantlrguy.atlassian.net/wiki/display/ANTLR3/Five+minute+introduction+to+ANTLR+3
Edit: I haven't used ANTLR in a while - it's now up to version 4, and there may be some significant differences, so make sure that you check the documentation for that version.

How to split two lines in java ? while i try to split by /n , the string is printing with garbage value :(

While I tried splite a line by below code. getting the result some garbage. while i printed the value of selectedproduct.get(j).getText() I'm getting the below string
Civil War A Nation Divided
Playstation2 Software
I just required the upper one.
System.out.println(selectedproduct.get(j).getText().split("\\n"));
String.split() returns an array of values between each occurrence of the delimiter. The reason you're getting "garbage" values is because arrays use the default implementation of toString(). To print the full array, you can use Arrays.toString():
System.out.println(Arrays.toString(selectedproduct.get(j).getText().split("\\n")));
If you only want the first line, just print the first element of the returned array:
System.out.println(selectedproduct.get(j).getText().split("\\n")[0]);
The .split() would create an array of strings so to get teh first part of splitted array use the index as you print.
System.out.println(selectedproduct.get(j).getText().split("\\n")[0]); //index of upper line for you
Also in some cases you might want to use \r\n for platform specific carriage return -
System.out.println(selectedproduct.get(j).getText().split("\\r\\n")[0]);

Java String split is not working

Java Experts ,
Please look into the below split command code and let me know why last two nulls are not captured.
String test = "1,O1,,,,0.0000,0.0000,,";
String[] splittest = test.split(",");
System.out.println("length -"+splittest.length);
for (String string : splittest) {
System.out.println("value"+string);
}
The result iam getting
length -7
value1
valueO1
value
value
value
value0.0000
value0.0000
surprisingly the length is 7 where as it should be 9 and also as you can see values after 0.0000 ie two last nulls are not coming . Lets say now if i change the string test
"1,O1,,,,0.0000,0.0000,0,0"
String test = "1,O1,,,,0.0000,0.0000,0,0";
String[] splittest = test.split(",");
System.out.println("length -"+splittest.length);
for (String string : splittest) {
System.out.println("value"+string);
}
I am getting correctly
length -9
value1
valueO1
value
value
value
value0.0000
value0.0000
value0
value0
I don't think iam doing wrong . Is it a bug ? JAVA Version - jdk1.6.0_31
It behaves as specified in the javadoc:
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
If you want to keep the trailing blank strings, you can use the 2 argument split method with a negative limit:
String[] splittest = test.split(",", -1);
If the limit is non-positive then the pattern will be applied as many times as possible and the array can have any length.
split silently discards trailing separators, as specified in the Javadoc.
In general, the behavior of split is kind of weird. Consider using Guava's Splitter instead, which has somewhat more predictable and customizable behavior. (Disclosure: I contribute to Guava.)
Splitter.on(',').split("1,O1,,,,0.0000,0.0000,,");
// returns [1, O1, , , , 0.0000, 0.0000, , ]
Splitter.on(',').omitEmptyStrings()
.split("1,O1,,,,0.0000,0.0000,,");
// returns [1, O1, 0.0000, 0.0000]
As mentioned above, test.split(","); will ignore trailing blank strings. You could use the two parameter method with a large second argument. However, the API also states
If n is non-positive then the pattern will be applied as many times
as possible and the array can have any length.
where n is the second argument. So if you want all the trailing strings, I would recommend
test.split(",", -1);

regular expression Help on - extract a value from a string ignoring the previous contents

can someone help me to write a java regex to retrieve a value from the following string please?
XX0001 15NOV XXX SELECTED RAX AXXXXX DXXXXXXXXX REF NBR 002 SSSS
I wanted to extract the value 002. All the strings / characters before 002 are fixed length and properly padded with trailing space (if req.). could have any string/numeric/special displayable characters.
I am looking for something like ... get 002 from that position ignoring whatever before. ?
What language?
If javascript:
myCode=myString.substring(56,58);
or
myCode=myString.substr(56,3);
If PHP:
$myCode=substr($myString,56,3);
This simpler option is preferable to regex because it is faster. You can use this because you're working with fixed length strings.
EDIT: Just saw your edit referencing Java. So in Java:
String myCode = myString.substring(56,59);
In Java
string.substring(56,59);
You don't need regex to do that. Just use the String method substring:
String myString = originalString.substring(106,109); // myString = "002"
106 is the begin index, and 109 the end index - 1. To simply get the first, just take the length of the original string just before the number you want to get, for instance:
System.out.println("XX0001 15NOV XXX SELECTED RAX AXXXXX DXXXXXXXXX REF NBR ".length());
Assuming that you want the last set of digits before the end of the string, you might want to do something like this:
^.*\\d{3}\\s+.{4}$
This should instruct the Regex Engine to start matching from the beginning of the string, match any characters and then, from the end, match 3 numbers, a space and any 4 characters.
Also, if you have fixed sizes and lengths, you can most likely get away with a .substring method, it is less complex.

How do I split a concatenated string into multiple floating point values?

I'm a begginer in java I have
packet=090209153038020734.0090209153039020734.0
like this I want to split this string and store into an array like two strings:
1) 090209153038020734.0
2) 090209153039020734.0
I have done like this:
String packetArray[] = packets.split(packets,Constants.SF);
Where:
Constants.SF=0x01.
But it won't work.
Please help me.
I'd think twice about using split since those are obviously fixed width fields.
I've seen them before on another question here (several in fact so I'm guessing this may be homework (or a popular data collection device :-)) and it's plain that the protocol is:
STX (0x01).
0x0f.
date (YYMMDD or DDMMYY).
time (HHMMSS).
0x02.
value (XXXXXX.X).
0x03.
0x04.
And, given that they're fixed width, you should probably just use substrings to get the information out.
The JavaDoc of String is helpful here: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html
You have your String packet;
String.indexOf(String) gives you a position of a special substring. your interested in the "." sign. So you write
int position = packet.indexOf(".")+1
+1 becuase you want the trailing decimal too. It will return something 20-ish and will be the last pos of the first number.
Then we use substring
String first = packet.substring(0,position) will give you everything up to the ".0"
String second = packet.substring(position-1) should give you everything starting after the ".0" and up to the end of the string.
Now if you want them explicitely into an array you can just put them there. The code as a whole - I may have some "off by one" -bugs.
int position = packet.indexOf(".")+1
String first = packet.substring(0,position)
String second = packet.substring(position-1)
String[] packetArray = new String[2];
packetArray[0] = first;
packetArray[1] = second;
String packetArray[] = packets.split("\u0001");
should work. You are using
public String[] split(String regex, int limit)
which is doing something else: It makes sure that split() returns an array with at most limit members (1 in this case, so you get what you ask for).
You need to read the Javadocs for the String.split() methods...you are calling the version of String.split() that takes a regular expression and a limit, but you are passing the string itself as the first parameter, which doesn't really make sense.
As Aaron Digulla mentioned, use the other version.
You don't say how you want to do the split. It could be based on a fixed length (number of characters) or you want one decimal place.
If the former you could do packetArray = new String[]{packet.substring(0, 20), packet.substring(21)};
int dotIndex = packets.indexOf('.');
packetArray = new String[]{packet.substring(0, dotIndex+2), packet.substring(dotIndex+2)};
Your solution confuses the regexp with the string.
split uses regular expressions as documented here. Your code seems to be trying to match the whole string Constants.SF = 0x01 times, which doesn't make much sense. If you know what char the boxes are then you can use something like {[^c]+cc} where c is the character of the box (i guess this is 0x01), to match each "packet".
I think you are trying to use it like the .net String.Split(...) function?

Categories