Strange parsing problem

A

Adam Lipscombe

Help!

I have a string like this

"_menuCloseDelay=500;\n_menuOpenDelay=100;\n_subOffsetTop=4;\n_subOffsetLeft=-10;\n"

The "\n" sections are not newlines - rather the '\' followed by the 'n' char. I want to replace this
char pair with a real line feed.

So far I have tried both StringTokenizer and split() without success:

---------------------------------------
String s = "_menuCloseDelay=500;\\n_menuOpenDelay=100;\\n_subOffsetTop=4;\\n_subOffsetLeft=-10;\\n";
StringBuilder b = new StringBuilder();
StringTokenizer st = new StringTokenizer(s, "\\n");
for (int j = 0 ; true == st.hasMoreTokens(); j++)
{
if (0 < j)
{
b.append('\n');
}
String tok = st.nextToken();
b.append(tok);
}

String s = b.toString();
-------------------------------------

-------------------------------------
String s = "_menuCloseDelay=500;\\n_menuOpenDelay=100;\\n_subOffsetTop=4;\\n_subOffsetLeft=-10;\\n";
String parts = s.split("\\n");
for (int j = 0 ; j < parts.length ; j++)
{
if (0 < j)
{
b.append('\n');
}
String tok = parts[j];
b.append(tok);
}

String s = b.toString();
-------------------------------------


I have tried "\n", "\\n", "\\\n", "\\\\n" as the delimiter but nothing works.


What am I doing wrong?


TIA - Adam
 
S

Simon

Adam said:
Help!

I have a string like this

"_menuCloseDelay=500;\n_menuOpenDelay=100;\n_subOffsetTop=4;\n_subOffsetLeft=-10;\n"


The "\n" sections are not newlines - rather the '\' followed by the 'n'
char. I want to replace this char pair with a real line feed.

So far I have tried both StringTokenizer

Stefan Ram already said why the implementation using StringTokenizer failed.

and split() without success:
String s =
"_menuCloseDelay=500;\\n_menuOpenDelay=100;\\n_subOffsetTop=4;\\n_subOffsetLeft=-10;\\n";

String parts = s.split("\\n");
for (int j = 0 ; j < parts.length ; j++)
{
if (0 < j)
{
b.append('\n');
}
String tok = parts[j];
b.append(tok);
}

String s = b.toString();

Are you sure you tried that? It doesn't compile since String.split() returns an
array. Try this line instead:

String parts = s.split("\\\\n");

Note the four \'s. String.split() expects a regular expression, so you have to
escape the backslash twice. This implementation works fine for me.

Cheers,
Simon
 
S

Simon

String parts = s.split("\\n");
Are you sure you tried that? It doesn't compile since String.split() returns an
array. Try this line instead:

String parts = s.split("\\\\n");

No, don't. It fixes only half of the problem :) This is what I meant to write:

String[] parts = s.split("\\\\n");
 
S

Sigfried

Adam Lipscombe a écrit :
Help!

I have a string like this

"_menuCloseDelay=500;\n_menuOpenDelay=100;\n_subOffsetTop=4;\n_subOffsetLeft=-10;\n"


The "\n" sections are not newlines - rather the '\' followed by the 'n'
char. I want to replace this char pair with a real line feed.

So far I have tried both StringTokenizer and split() without success:

---------------------------------------
String s =
"_menuCloseDelay=500;\\n_menuOpenDelay=100;\\n_subOffsetTop=4;\\n_subOffsetLeft=-10;\\n";

StringBuilder b = new StringBuilder();
StringTokenizer st = new StringTokenizer(s, "\\n");
for (int j = 0 ; true == st.hasMoreTokens(); j++)
{
if (0 < j)
{
b.append('\n');
}
String tok = st.nextToken();
b.append(tok);
}

String s = b.toString();
-------------------------------------

-------------------------------------
String s =
"_menuCloseDelay=500;\\n_menuOpenDelay=100;\\n_subOffsetTop=4;\\n_subOffsetLeft=-10;\\n";

String parts = s.split("\\n");
for (int j = 0 ; j < parts.length ; j++)
{
if (0 < j)
{
b.append('\n');
}
String tok = parts[j];
b.append(tok);
}

String s = b.toString();
-------------------------------------


I have tried "\n", "\\n", "\\\n", "\\\\n" as the delimiter but nothing
works.


What am I doing wrong?


TIA - Adam

Why not String.replace("\\n", "\n") ?
 
T

The ScuzzBuster

Help!

I have a string like this

"_menuCloseDelay=500;\n_menuOpenDelay=100;\n_subOffsetTop=4;\n_subOffsetLeft=-10;\n"

The "\n" sections are not newlines - rather the '\' followed by the 'n' char. I want to replace this
char pair with a real line feed.

So far I have tried both StringTokenizer and split() without success...

And there followed lots of fairly complex and hairy ways to do it, and
finally one simple-but-inefficient one (String.replace()).

I had this exact problem the other day and I just rattled out
something like:

StringBuilder sb = new StringBuilder();
boolean lastWasBackslash = false;
for (int i = 0; i < string.length(); ++i) {
char ch = string.charAt(i);
if (lastWasBackslash) {
if (ch == '\\') sb.append(ch);
else if (ch == 'n') sb.append('\n');
else { sb.append('\\'); sb.append(ch); }
} else {
if (ch == '\\') lastWasBackslash = true;
else { sb.append(ch); lastWasBackslash = false; }
}
}
String result = sb.toString();

It's more code, but it's also fast code, probably as fast as is
possible in Java for this job. It's also easy to extend the above code
to add more escape sequences, or to modify it to throw an exception if
there's a bogus backslash escape (change the first "else" clause to
throw, and remember to also throw if lastWasBackslash is true just
after the loop terminates).

(Java developers, please add the ability to iterate strings! for (char
ch : string) would be so much nicer.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,160
Latest member
CollinStri
Top