简体   繁体   中英

Split and Crop a string array in 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.

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 double s 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)

This regex based code might work for you:

String str = "A/46\\-54.67/48\\-6.94/52\\2.41/54\\0.00/38\\-0.18/34\0.06/32\\-27.22/38";
Pattern p = Pattern.compile("\\\\(-?\\d+\\.\\d+)/(\\d+)");
Matcher m = p.matcher(str);
while (m.find()) {
    System.out.printf("Before: %s, After: %s%n", m.group(1), m.group(2));
}

OUTPUT:

Before: -54.67, After: 48
Before: -6.94, After: 52
Before: 2.41, After: 54
Before: 0.00, After: 38
Before: -0.18, After: 34
Before: -27.22, After: 38

EDIT: To put these matched values in ArrayLists:

String str = "A/46\\-54.67/48\\-6.94/52\\2.41/54\\0.00/38\\-0.18/34\0.06/32\\-27.22/38";
Pattern p = Pattern.compile("\\\\(-?\\d+\\.\\d+)/(\\d+)");
Matcher m = p.matcher(str);
List<Double> beforeList = new ArrayList<Double>();
List<Integer> afterList = new ArrayList<Integer>();
while (m.find()) {
    beforeList.add(Double.parseDouble(m.group(1)));
    afterList.add(Integer.parseInt(m.group(2)));
}
System.out.printf("Before: %s, After: %s%n", beforeList, afterList);

I typed this in without using the IDE so there may be some minor mistakes:

//Split at /
String[] parts = udpResponse.split("/");

//for each part (apart from the initial A)
for (int i=1; i<parts.length; i++) {

   //the part is of the form aaa\bbb where aaa and bbb are numbers
   String[] keyValue = parts[i].split("\\");
   double key = Double.parseDouble(keyValue[0]); //number before \
   double value = Double.parseDouble(keyValue[1]); //number after \

   //do whatever you like with the numbers... 
}

Try this

   String test="A/46\\-54.67/48\\-6.94/52\\2.41/54\\0.00/38\\-0.18/34\\0.06/32\\-27.22";
   String[] arr=test.split("\\\\");
   double[] dub=new double[arr.length];
   int k=0;
   for (String i:arr){
        if(i.split("/").length!=1) {
        dub[k]=Double.parseDouble(i.split("/")[1].trim());
        k++;
        }
   }

I recommend that you use String.split() . This is a handy function for relatively simple string parsing such as your example here. First you need to split the UDP reply on the '/'. It looks like you ignore the first and last String s in the resulting array. For the remaining String s, you can use a for loop to call String.split() on each of them in turn, splitting on the '\\' character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM