Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split and Crop a string array in Java

Tags:

java

I have a little problem with a String. The String looks like this one:

A/46\-54.67/48\-6.94/52\2.41/54\0.00/38\-0.18/34\0.06/32\-27.22 (...)

It's an answer from a UDP-Request. The Request was like: R/46/48(...)/32(...) , it means: give me the value for #46,#48 ... The answer is shown above.

Aim:

Finally I only want the specific values as a double from the String for each request.

My favorite would be a class with 3(or 2) args. The first argument will be the String it self, the second would be the position inside the String (like Value after 46\ ). And, if needed , a third for the end of the Value, like /. It returns a array with the values.

(Not a real Solution for me: I can only request on Value at once, so the answer will be easier to work with. Problem, if have to do much more UDP Requests per Time. Current the Request will be 10 Times a Second, ultimatley it will be much more often. And i have to calculate with the values)

Problem:

The String is not consistent. The returned Values can have different digits and can be signed or not. Consistent is only: A/46\(digit for value 46)/48\(digit)/52\(digit)/54\(digit)/38\_/34\_/32\_ As you can see, the Value is between "\ /". I got a "solution" that cuts the Values a specific positions. But it failed when the digits changed.

Solution?

(Yes, I'm a real Java Greenhorn) My tries gave me a array that looks like this one:

A/46

-54.67/48

-6.94/52

2.41/54

0.00/38

-0.18/34

0.06/32

-27.22

So, and now i don't have a clue how to get the last part off the Value to convert them it into a double(array).

I hope I've described my Problem vividly.

Thanks for your comments and suggestions.

like image 719
Peter Avatar asked Jul 09 '13 15:07

Peter


People also ask

Can you split a string array in Java?

Split() String method in Java with examples. The string split() method breaks a given string around matches of the given regular expression. After splitting against the given regular expression, this method returns a string array.

Can you split a string array?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

How Use trim and split in Java?

To remove extra spaces before and/or after the delimiter, we can perform split and trim using regex: String[] splitted = input. trim(). split("\\s*,\\s*");


1 Answers

Start by splitting with /; you will get a string array looking like:

[ "A", "46\\-54.67", ... ]

Forget the first element; then, for each other element, do:

Double.parseDouble(theElement.subString(theElement.indexOf('\\')))

This will get you the double you want; all you have left to do is get these doubles where you want them.

Another solution is to cycle through the string with this regex:

(\d+)\\(-?\d+\.\d+)

The first group of the regex captures the integer, the second captures the double:

final Pattern p = Pattern.compile("(\\d+)\\\\(-?\\d+\\.\\d+)");

final Matcher m = p.matcher(resultString);

while (m.find())
     // deal with m.group(1), m.group(2)
like image 198
fge Avatar answered Sep 28 '22 16:09

fge