java - String.split() not working as intended -


i'm trying split string, however, i'm not getting expected output.

string 1 = "hello 0xa0xagoodbye"; string two[] = one.split(" |0xa"); system.out.println(arrays.tostring(two)); 

expected output: [hello, goodbye]

what got: [hello, , , goodbye]

why happening , how can fix it?

thanks in advance! ^-^

this result caused multiple consecutive matches in string. may wrap pattern grouping construct , apply + quantifier match multiple matches:

string 1 = "hello 0xa0xagoodbye"; string two[] = one.split("(?:\\s|0xa)+"); system.out.println(arrays.tostring(two)); 

a (?:\s|0xa)+ regex matches 1 or more whitespace symbols or 0xa literal character sequences.

see java online demo.

however, still empty value first item in resulting array if 0xa or whitespaces appear @ start of string. then, have remove them first:

string two[] = one.replacefirst("^(?:\\s|0xa)+", "").split("(?:\\s+|0xa)+"); 

see another java demo.


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

python - Error: Unresolved reference 'selenium' What is the reason? -

asp.net ajax - Jquery scroll to element just goes to top of page -