JSP substring and trim don't work properly

N

nino9stars

Hello,

I am trying to do (what I think) is a simple task. I am trying to take
in a list of information, parse through it, and display it. However,
for some reason, the trim() String function isn't working right. I am
using JSDK 1.4 on Tomcat 4.0 (if that matters)...

A sample of the information that is sent:

Jon Stewart <[email protected]>, Jane Doe <[email protected]>, (e-mail address removed),

With that, I want to parse the information into an array with no
leading or trailing spaces... Here is my code:

//We have to look through the actual inputted information
String[] s_final = new String[2];
String[] NONList = request.getParameter("gnon").split(",");
for (int x=0; x < NONList.length; x++) {
String name = "";
String nameTrim = "";
String email = "";

if (s_string.indexOf("<") != -1) {
//There is a name and an email

//It used to be like this... but it didn't work either...
//name = NONList[x].substring(0, NONList[x].indexOf("<")).trim();
name = NONList[x].substring(0, NONList[x].indexOf("<"));
nameTrim = name.trim();

email = NONList[x].substring(NONList[x].indexOf("<")+1,
NONList[x].indexOf(">"));

s_final[0] = nameTrim;
s_final[1] = email;

} else {
//There is only an email
email = NONList[x].trim();
s_final[0] = "";
s_final[1] = email;
}

out.println("Name: -"+tmp[0]+"-<BR>");
out.println("Email: -"+tmp[1]+"-<BR>");
}

I am expecting to get an output like this:
Name: -Jon Stewart-
Email: (e-mail address removed)-
Name: -Jane Doe-
Email: (e-mail address removed)-
Name: --
Email: (e-mail address removed)-

Instead I am getting the following output:
Name: -Jon Stewart -
Email: (e-mail address removed)-
Name: - Jane Doe -
Email: (e-mail address removed)-
Name: --
Email: - (e-mail address removed)-

In case it's hard to tell, I am trying to point out that there are
spaces in front and after most of the names and emails. I thought that
is what trim() was supposed to get rid of????

I am seriously baffled. Any suggestions, or comments are greatly
appreciated. Maybe if there is even a better way to get to my result.

Let me know if anything is confusing.

Thanks in advance,
Nino Skilj
 
S

Soren Kuula

A sample of the information that is sent:

Jon Stewart <[email protected]>, Jane Doe <[email protected]>, (e-mail address removed),

With that, I want to parse the information into an array with no
leading or trailing spaces... Here is my code:

You should write your code in a way that makes bugs less probable... for
example, if you decided to trim everything, make the variables that hold
untrimmed values unreachable wherever possible.
//We have to look through the actual inputted information
String[] s_final = new String[2];
String[] NONList = request.getParameter("gnon").split(",");
for (int x=0; x < NONList.length; x++) {
// DANGEROUS: You might make the mistake of using this, asnd not
nameTrim, later on. Remove.
String name = "";
String nameTrim = "";
String email = "";

if (s_string.indexOf("<") != -1) {
//There is a name and an email

//It used to be like this... but it didn't work either...
//name = NONList[x].substring(0, NONList[x].indexOf("<")).trim();
//name = NONList[x].substring(0, NONList[x].indexOf("<"));
> String name = NONList[x].substring(0, NONList[x].indexOf("<"));
nameTrim = name.trim();

email = NONList[x].substring(NONList[x].indexOf("<")+1,
NONList[x].indexOf(">"));

You forget to trim the email here.
s_final[0] = nameTrim;
s_final[1] = email;

} else {
//There is only an email
email = NONList[x].trim();
s_final[0] = "";
s_final[1] = email;
}


In case it's hard to tell, I am trying to point out that there are
spaces in front and after most of the names and emails. I thought that
is what trim() was supposed to get rid of????

Yes. ARE they spaces, or du they just look like? There are also
nonbreaking spaces .... try to alter
> out.println("Name: -"+tmp[0]+"-<BR>");
> out.println("Email: -"+tmp[1]+"-<BR>");
to

> out.println("Name: -"+truth(tmp[0])+"-<BR>");
> out.println("Email: -"+truth(tmp[1])+"-<BR>");

and define (not tested, might have errors, but you can fix'em ;)

String truth(String s) {
char[] chars = s.toCharArray();
StringBuffer sb = new StringBuffer();
for (int i=0; i<chars.length;i++){
char c = chars;
if (c >= 'a' && c <= 'z' || c >= '>' && c <= 'Z')
sb.append(c);
else
sb.append("[code:" + c + "]");
}
}

See if all the spaces are really spaces? (code 32)

Søren
 
N

nino9stars

Soren said:
(e-mail address removed) wrote:
You should write your code in a way that makes bugs less probable... for
example, if you decided to trim everything, make the variables that hold
untrimmed values unreachable wherever possible.

Yeah, I know. A lot of the "extra" stuff was just trying to make things
work after getting frustrated...
In case it's hard to tell, I am trying to point out that there are
spaces in front and after most of the names and emails. I thought that
is what trim() was supposed to get rid of????

Yes. ARE they spaces, or du they just look like? There are also
nonbreaking spaces .... try to alter
out.println("Name: -"+tmp[0]+"-<BR>");
out.println("Email: -"+tmp[1]+"-<BR>"); to
out.println("Name: -"+truth(tmp[0])+"-<BR>");
out.println("Email: -"+truth(tmp[1])+"-<BR>");

and define (not tested, might have errors, but you can fix'em ;)

String truth(String s) {
char[] chars = s.toCharArray();
StringBuffer sb = new StringBuffer();
for (int i=0; i<chars.length;i++){
char c = chars;
if (c >= 'a' && c <= 'z' || c >= '>' && c <= 'Z')
sb.append(c);
else
sb.append("[code:" + c + "]");
}
}

See if all the spaces are really spaces? (code 32)


I put in your code (which ran perfectly by the way...) to see if they
really were spaces. They do appear to be so. The information showed up
on screen as [code: ]... So I assume that is correct? However, when I
went to add the information into the database it did show up as a funky
character? What else could the information be interpreted as besides a
space? Is there something specific I can look for and omit? I thought
that was what trim was supposed to do?

Thanks for your help. Hopefully I can get this working!

Nino Skilj
 
M

Mark Space

character? What else could the information be interpreted as besides a
space? Is there something specific I can look for and omit?

Um, maybe if you'd tell us what code # is being printed out....
 
N

nino9stars

Mark said:
Um, maybe if you'd tell us what code # is being printed out....

I am not sure what you mean... On screen it printed out like this:

[code: ][code: ]John[code: ]Smith[code: ]

just like that... There was nothing else? Am I missing something? I
ended up modifying your code a bit and allowing a space character like
this:

char[] nameChars = name.toCharArray();
StringBuffer sbName = new StringBuffer();
for (int i=0; i< nameChars.length; i++) {
char c = nameChars;
if ((c >= 'a' && c <= 'z') | (c >= 'A' && c <= 'Z') | c==' ') {
sbName.append(c);
} else {
sbName.append("code[" + c + "]");
}
}

and this is what it returned...

[code: ][code: ]John Smith[code: ]

So it doesn't say what the character was, but it still looks like a
space on screen?

Nino
 
I

Ian Wilson

Mark said:
(e-mail address removed) wrote:



Um, maybe if you'd tell us what code # is being printed out....


I am not sure what you mean... On screen it printed out like this:

[code: ][code: ]John[code: ]Smith[code: ]

just like that... There was nothing else? Am I missing something? I
ended up modifying your code a bit and allowing a space character like
this:

char[] nameChars = name.toCharArray();
StringBuffer sbName = new StringBuffer();
for (int i=0; i< nameChars.length; i++) {
char c = nameChars;
if ((c >= 'a' && c <= 'z') | (c >= 'A' && c <= 'Z') | c==' ') {


This assumes ASCII of course.
sbName.append(c);
} else {
sbName.append("code[" + c + "]");

But Soren's suggested line was
sb.append("[code:" + c + "]");
note the position of "[" before "code"
}
}

and this is what it returned...

[code: ][code: ]John Smith[code: ]

That suggests that your code uses Soren's suggestion and that instead of
cutting and pasting you are retyping. This would mean that newsgroup
readers are being deceived into thinking they are seeing your real code
when they are not. It's best to cut and paste.

If you were using Soren's code then your "space" character is not an
ASCII space but a single character that looks like the two character
sequence ": " (colon, space) when displayed to you. Could it be that you
have mismatched encodings (e.g. UTF-8 vs Latin-1 etc)

Or maybe you have the colon in the real code and not in the code you posted?

As I said, I think it's best to cut and paste code into news group
postings, not re-type them.
So it doesn't say what the character was, but it still looks like a
space on screen?

Try this, then an ascii space would show as [code:32]
sb.append("[code:" + (int)c + "]");



--------------------------8<------------------------
public class NonAscii {
public static void main(String[] args) {
String s = "foo bar";
char[] chars = s.toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < chars.length; i++) {
char c = chars;
if (c >= 'a' && c <= 'z' || c >= '>' && c <= 'Z')
sb.append(c);
else
sb.append("[code:" + (int)c + "]");
}
System.out.println("sb='"+sb+"'");
}
}
------------------------8<----------------------------
sb='foo[code:32]bar'
 
N

nino9stars

But Soren's suggested line was
sb.append("[code:" + c + "]");
note the position of "[" before "code"
and this is what it returned...

[code: ][code: ]John Smith[code: ]

That suggests that your code uses Soren's suggestion and that instead of
cutting and pasting you are retyping. This would mean that newsgroup
readers are being deceived into thinking they are seeing your real code
when they are not. It's best to cut and paste.

If you were using Soren's code then your "space" character is not an
ASCII space but a single character that looks like the two character
sequence ": " (colon, space) when displayed to you. Could it be that you
have mismatched encodings (e.g. UTF-8 vs Latin-1 etc)

Or maybe you have the colon in the real code and not in the code you posted?

As I said, I think it's best to cut and paste code into news group
postings, not re-type them.

You're right. I'm sorry. I actually had removed that original else
statement, added the c==' ' value (for a space) to the original code
and it ended up working how I intended trim() to work. I was trying to
just retype to get the message out quickly. Sorry about that...
So it doesn't say what the character was, but it still looks like a
space on screen?

Try this, then an ascii space would show as [code:32]
sb.append("[code:" + (int)c + "]");

--------------------------8<------------------------
public class NonAscii {
public static void main(String[] args) {
String s = "foo bar";
char[] chars = s.toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < chars.length; i++) {
char c = chars;
if (c >= 'a' && c <= 'z' || c >= '>' && c <= 'Z')
sb.append(c);
else
sb.append("[code:" + (int)c + "]");
}
System.out.println("sb='"+sb+"'");
}
}
------------------------8<----------------------------
sb='foo[code:32]bar'


I tried this and the code for the blanks in front of the name and email
was (hold on... I'm copying and pasting)...
[code:160]Jon[code:32]Smith[code:160]

So, I guess the next question is what is code:160 ??? I guess trim()
wouldn't know how to handle that? Could it have something to do with
being submitted by a post over the web?

I got it working how I wanted to, but now I'm just interested in
learning why...

Thanks for all your help!

Nino
 
N

nino9stars

But Soren's suggested line was
sb.append("[code:" + c + "]");
note the position of "[" before "code"
and this is what it returned...

[code: ][code: ]John Smith[code: ]

That suggests that your code uses Soren's suggestion and that instead of
cutting and pasting you are retyping. This would mean that newsgroup
readers are being deceived into thinking they are seeing your real code
when they are not. It's best to cut and paste.

If you were using Soren's code then your "space" character is not an
ASCII space but a single character that looks like the two character
sequence ": " (colon, space) when displayed to you. Could it be that you
have mismatched encodings (e.g. UTF-8 vs Latin-1 etc)

Or maybe you have the colon in the real code and not in the code you posted?

As I said, I think it's best to cut and paste code into news group
postings, not re-type them.

You're right. I'm sorry. I actually had removed that original else
statement, added the c==' ' value (for a space) to the original code
and it ended up working how I intended trim() to work. I was trying to
just retype to get the message out quickly. Sorry about that...
So it doesn't say what the character was, but it still looks like a
space on screen?

Try this, then an ascii space would show as [code:32]
sb.append("[code:" + (int)c + "]");

--------------------------8<------------------------
public class NonAscii {
public static void main(String[] args) {
String s = "foo bar";
char[] chars = s.toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < chars.length; i++) {
char c = chars;
if (c >= 'a' && c <= 'z' || c >= '>' && c <= 'Z')
sb.append(c);
else
sb.append("[code:" + (int)c + "]");
}
System.out.println("sb='"+sb+"'");
}
}
------------------------8<----------------------------
sb='foo[code:32]bar'


I tried this and the code for the blanks in front of the name and email
was (hold on... I'm copying and pasting)...
[code:160]Jon[code:32]Smith[code:160]

So, I guess the next question is what is code:160 ??? I guess trim()
wouldn't know how to handle that? Could it have something to do with
being submitted by a post over the web?


I think I figured out the root of the problem... When I was submitting
information in the code, apparently I had used &nbsp; to keep my
spacing appropriate. I assumed this would be considered a space when
sent over the line. My guess is that a null blank space is different
from a regular space in how trim() handles that information. I haven't
had time to test to see if trim() works now that I took out those null
blank spaces, but when I do, I will be sure to post again.

Thanks again so much for helping. I don't think I would have figured
out the solution if I wasn't pointed in the right direction!

Nino
 
I

Ian Wilson

So it doesn't say what the character was, but it still looks like a
space on screen?


Try this, then an ascii space would show as [code:32]
sb.append("[code:" + (int)c + "]");

I tried this and the code for the blanks in front of the name and email
was (hold on... I'm copying and pasting)...
[code:160]Jon[code:32]Smith[code:160]

So, I guess the next question is what is code:160 ???

Code point 160 is Unicode character NBSP, the "no-break space" also
known as "non-breaking space".
http://en.wikipedia.org/wiki/Non-breaking_space

Sun's definition of the "whitespace" that trim() trims appears to be
just \u0000 to \u0020 (i.e. [code:0] to [code:32] in decimal).

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#trim()

I think I figured out the root of the problem... When I was submitting
information in the code, apparently I had used &nbsp; to keep my
spacing appropriate. I assumed this would be considered a space when
sent over the line. My guess is that a null blank space is different

Minor correction: &nbsp; is an abbreviation of *non-breaking* space.
from a regular space in how trim() handles that information. I haven't
had time to test to see if trim() works now that I took out those null
blank spaces, but when I do, I will be sure to post again.

Thanks again so much for helping. I don't think I would have figured
out the solution if I wasn't pointed in the right direction!

You're welcome. Interesting problem.
 
S

Soren Kuula

Soren said:
(e-mail address removed) wrote:

You should write your code in a way that makes bugs less probable... for
example, if you decided to trim everything, make the variables that hold
untrimmed values unreachable wherever possible.


Yeah, I know. A lot of the "extra" stuff was just trying to make things
work after getting frustrated...

In case it's hard to tell, I am trying to point out that there are
spaces in front and after most of the names and emails. I thought that
is what trim() was supposed to get rid of????

Yes. ARE they spaces, or du they just look like? There are also
nonbreaking spaces .... try to alter
out.println("Name: -"+tmp[0]+"-<BR>");
out.println("Email: -"+tmp[1]+"-<BR>"); to
out.println("Name: -"+truth(tmp[0])+"-<BR>");
out.println("Email: -"+truth(tmp[1])+"-<BR>");

and define (not tested, might have errors, but you can fix'em ;)

String truth(String s) {
char[] chars = s.toCharArray();
StringBuffer sb = new StringBuffer();
for (int i=0; i<chars.length;i++){
char c = chars;
if (c >= 'a' && c <= 'z' || c >= '>' && c <= 'Z')
sb.append(c);
else
sb.append("[code:" + c + "]");
}
}

See if all the spaces are really spaces? (code 32)



I put in your code (which ran perfectly by the way...) to see if they
really were spaces. They do appear to be so. The information showed up
on screen as [code: ]... So I assume that is correct?


I should have made that:

sb.append("[code:" + Integer.toString(c) + "]");

However, when I
went to add the information into the database it did show up as a funky
character? What else could the information be interpreted as besides a
space? Is there something specific I can look for and omit? I thought
that was what trim was supposed to do?

32 is space. Other numbers are other things.

Søren
 

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

Members online

No members online now.

Forum statistics

Threads
474,265
Messages
2,571,069
Members
48,771
Latest member
ElysaD

Latest Threads

Top