Java: Replace spaces in a String with a different whitespace character in? -


i have massive text file have manually parse. there no other way iterate on it.

i'm grabbing each of lines in file , .split(" ") them individual components - int arrays, others char arrays, , other actual text strings.

the text strings causing me headache because have space in there.

an example line like:

string strline = "identifier {2 4 \"#0# == \\\"this string\\\"\" 12 21 6} 

when following:

string[] strparts = strline.split(" "); 

the resulting output string array has string values of:

identifier,{2,4,"#0#,==,\"this,string\"",12,21,6} 

i need output be:

identifier,{2,4,"#0# == \"this string\"",12,21,6} 

so i'm pondering if there different whitespace character can apply string inside quotes prior executing split().

anyone know of one?

i considered diving regex, haven't worked regex enough able formulate logic around split - split on space unless space between first , last quote".

thx.

[update]

i adding here because formatting of code in replies not optimal.

string strline = "identifier {2 4 \"#0# == \\\"this string\\\"\" 12 21 6}";  string delim = "§"; //use section sign delimeter      stringbuilder sb = new stringbuilder();     //first part     sb.append(new string(strline.substring(0,strline.indexof("\""))));      //middle part     sb.append(new string(strline.substring(strline.indexof("\""),strline.lastindexof("\""))).replace(" ", delim));      //last part     sb.append(new string(strline.substring(strline.lastindexof("\""))));      //make array     string[] parts = sb.tostring().split(" "); 

i'll need replace delimeter chars later on @ least need now.

thanks suggestions, combination of them solved me.

you don't need replace string spaces space. try unique like: '_!_!'

look substitution string first verify it's not in file, substitution.

then normal split.

and, finally, replace substitution string normal space in finished product.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -