简体   繁体   中英

Strange Behavior when Splitting a String

I am trying to split a string using a delimiter and I'm getting some odd results when I grab values from different cells in the String array. Example:

dataString = (String) hashMap.get("LCSSAMPLEREQUEST_sampleRequestString");

System.out.println(dataString);

String dataStringSplit[] = dataString.split("quantity|&^&|");

String tempString = dataStringSplit[0];

Here is the line in dataString before the first delimiter:

"sortingNumber|&^&|1|-()-|ID|&^&|1|-()-|DROPPED|&^&|false|-()-|"

Now when I do a System.out of 'tempString' , I get a string with no value.

If I do tempString = dataStringSplit[1] , then I get a value of 'sor' .

What am I doing wrong here?

"quantity|&^&|" should be "quantity\\\\|&\\\\^&\\\\|" . | and ^ are a meta character in regex. in order to treat it as a normal charcter you should escape it with backslash

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