substring() not working?

P

Peter Parker

I am using JDK 1.5. I tried to use substring() to extract lastname,
firstname, fullname, etc. from the following 'sample' string. For some
reason, the substring() is NOT working right. Please help. Also, what's the
best way to extract fields from a string (or from a file) like this? java/VB
Script? PERL? Thanks.String sample = "<a
href="http://email.people.yahoo.com/py/ps...de=US&amp;[email protected]&amp;URL=">vCard</a>";String
firstName = sample.substring(sample.indexOf("FirstName=") +10,
sample.indexOf("&amp;LastName"));String lastName =
sample.substring(sample.indexOf("LastName=") +09,
sample.indexOf("&amp;FullName"));String fullName =
sample.substring(sample.indexOf("FullName=") +10,
sample.indexOf("&amp;City"));.....
 
A

Andrew Thompson

Peter said:
I am using JDK 1.5. I tried to use substring() to extract lastname,
firstname, fullname, etc. from the following 'sample' string. For some
reason, the substring() is NOT working right.

So..
a) What's it doing?
b) What do you expect it to do?
c) Can you provide a *compilable* code example that demonstrates?
firstName = sample.substring(sample.indexOf("FirstName=") +10,
sample.indexOf("&amp;LastName"));String lastName =
sample.substring(sample.indexOf("LastName=") +09,
sample.indexOf("&amp;FullName"));String fullName =
sample.substring(sample.indexOf("FullName=") +10,
sample.indexOf("&amp;City"));.....

And as an aside.. Please refrain from compacting code like this,
it makes it very hard to read (and does not make it 'short')..

Andrew T.
 
P

Peter Parker

It gives "String index out of range" error. It seems as if '&amp;' is not
recognized by String.indexOf(), or anything with ';' in front, for that
matter. If I use '&amp' (without ';'), as in

firstName = sample.substring(sample.indexOf("FirstName=") +10,
sample.indexOf("&amp"));

it only worked with the firstName

if I use

String lastName = sample.substring(sample.indexOf("LastnameName=") +10,
sample.indexOf("FullName="));

it gives "String index out of range" error.

Thanks.

Peter Parker said:
I am using JDK 1.5. I tried to use substring() to extract lastname,
firstname, fullname, etc. from the following 'sample' string. For some
reason, the substring() is NOT working right. Please help. Also, what's the
best way to extract fields from a string (or from a file) like this? java/VB
Script? PERL? Thanks.String sample = "<a
href="http://email.people.yahoo.com/py/psEmailVcard.vcf?Pyt=Tps&amp;srch=ema
il&amp;ext=vcard.vcf&amp;FirstName=Bush&amp;LastName=George&amp;FullName=Geo
rge+Bush&amp;City=Washington&amp;State=DC&amp;Country=US&amp;CountryCode=US&
 
R

Ralf Seitner

Peter said:
It gives "String index out of range" error. It seems as if '&amp;' is not
recognized by String.indexOf(), or anything with ';' in front, for that
matter. If I use '&amp' (without ';'), as in

firstName = sample.substring(sample.indexOf("FirstName=") +10,
sample.indexOf("&amp"));

it only worked with the firstName

if I use

String lastName = sample.substring(sample.indexOf("LastnameName=") +10,
sample.indexOf("FullName="));

it gives "String index out of range" error.

Thanks.
Hi!
Have a look at the Java API.
The method throws an IndexOutOfBoundsException, if either
endIndex<beginIndex, beginIndex is negative or if endIndex >
sample.length().

So probably your beginIndex is too high (you add +10...)
Probably one String is not found, then indexOf returns -1, if I remember
correct and this would cause an IndexOutOfBoundsException in substring.

bye, Ralf
 
R

Ralf Seitner

Peter said:
It gives "String index out of range" error. It seems as if '&amp;' is not
recognized by String.indexOf(), or anything with ';' in front, for that
matter. If I use '&amp' (without ';'), as in

firstName = sample.substring(sample.indexOf("FirstName=") +10,
sample.indexOf("&amp"));

it only worked with the firstName

if I use

String lastName = sample.substring(sample.indexOf("LastnameName=") +10,
sample.indexOf("FullName="));
Perhaps "LastnameName=" is not found... isnt it "Lastname=" ?
 
A

Andrew Thompson

Peter Parker wrote:

Hello. Can you explain to me why I also got this
message in my email, from a person who calls
themselves 'Thomas'? Is that you?

If yes, I need to make something clear. Do not send me
email. Posting messages to Usenet newsgroups should
not be seen as an invitation to strike up a private
conversation.

And while I'm on the subject..
People will generally have more respect and time for
somebody who calls themselves by their *own* name.

If my guess is correct, Peter/Thomas are the same
person, so one name has to be ..wrong.

People might have any number of reasons not to
use their own name, when posting to a public
forum (for fear of racism or sexism, for example),
but please choose *one* name and stick to it.

Even more importantly, please do not use a name
that implies you are from a *different* culture/race/sex
than what you actually are.

If you wish to use a false name, please make it an
*obviously* false name - so that people do not jump
to the wrong conclusions about the words and
concepts that you will understand.

Andrew T.
 
M

Mark Space

Peter said:
Also, what's the
best way to extract fields from a string (or from a file) like this? java/VB
Script? PERL? Thanks.String sample = "<a
href="http://email.people.yahoo.com/py/psEmailVcard.vcf?Pyt=Tps&amp;srch=email&amp; snipped
String firstName = sample.substring(sample.indexOf("FirstName=") +10,
sample.indexOf("&amp;LastName"));

Hmm, that's a good question. I assumed the answer would be "use
java.net.URL" but there's no way to parse the query string. You can use
getQuery() to get the whole thing, which seems a bit safer than your method.
 
I

IchBin

Mark said:
Hmm, that's a good question. I assumed the answer would be "use
java.net.URL" but there's no way to parse the query string. You can use
getQuery() to get the whole thing, which seems a bit safer than your
method.

You can look at this code. It has a trace option at the top. I set it to
true. It will do what you want but I am not happy with it. I think I can
clean it up and make it tighter. Anyway, here ya go:

public class Parsetest
{
static boolean trace = true;
public static void main(String[] args)
{
String sample = "<a href="
+
"http://email.people.yahoo.com/py/ps...de=US&amp;[email protected]&amp;URL="
+ ">vCard</a>";
String firstName = "", lastName = "", fullName = "";
String city = "", state = "", country = "", countryCode = "",
email = "";

sample = sample.replace("&amp", "");
sample = sample.replace("=", " ");
String[] token = sample.split(";");
int i = 0;

for (i = 0; i < token.length; i++){
if (trace)
System.out.println("Queued (length, value) " +
token.length() + " "+ token);

if (token.length()> 3 &
token.substring(0, 4).equals("City")){
city = token.substring(4).trim();
} else if (token.length()> 4 &
token.substring(0, 5).equals("Email")){
email = token.substring(5).trim();
} else if (token.length()> 4 &
token.substring(0, 5).equals("State")){
state = token.substring(5).trim();
} else if (token.length()> 6 &
token.substring(0, 7).equals("Country")){
country = token.substring(7).trim();
} else if (token.length()> 7 &
token.substring(0, 8).equals("LastName")){
lastName = token.substring(8).trim();
} else if (token.length()>7 &
token.substring(0, 8).equals("FullName")){
fullName = token.substring(8).trim();
fullName = fullName.replace("+", " ");
} else if (token.length()> 8 &
token.substring(0, 9).equals("FirstName")){
firstName = token.substring(9).trim();
} else if (token.length() > 9 &
(token.substring(0, 10).equals("CountryCode"))) {
countryCode = token.substring(11).trim();
} else
if (trace)
System.out.println("Not Used: " + token);
}
// test
System.out.println("firstName: " + firstName);
System.out.println("lastName: " + lastName);
System.out.println("fullName: " + fullName);
System.out.println("city: " + city);
System.out.println("state: " + state);
System.out.println("country: " + country);
System.out.println("Country Code: " + state);
System.out.println("Email: " + email);

}
}
 
I

IchBin

Mark said:
Hmm, that's a good question. I assumed the answer would be "use
java.net.URL" but there's no way to parse the query string. You can use
getQuery() to get the whole thing, which seems a bit safer than your
method.

You can look at this code. It has a trace option at the top. I set it to
true. It will do what you want but I am not happy with it. I think I can
clean it up and make it tighter. Anyway, here ya go. This may get you
started in the right direction.

Here is test output with trace of the data you submitted in you message:
passing.

Not Used: ext vcard.vcf
Queued (length, value) 16 FirstName George
Queued (length, value) 13 LastName Bush
Queued (length, value) 20 FullName George+Bush
Queued (length, value) 15 City Washington
Queued (length, value) 8 State DC
Queued (length, value) 10 Country US
Queued (length, value) 14 CountryCode US
Queued (length, value) 26 Email (e-mail address removed)
Queued (length, value) 14 URL >vCard</a>
Not Used: URL >vCard</a>
firstName: George
lastName: Bush
fullName: George Bush
city: Washington
state: DC
country: Code US
Country Code: DC
Email: (e-mail address removed)


Here is the code:

public class Parsetest
{
// Set to display some trace information
static boolean trace = true;

public static void main(String[] args)
{
int i = 0;
String sample = "<a href="
+
"http://email.people.yahoo.com/py/ps...de=US&amp;[email protected]&amp;URL="
+ ">vCard</a>";
String firstName = "", lastName = "", fullName = "";
String city = "", state = "", country = "", countryCode = "",
email = "";

//
// Delete some inital stuff
sample = sample.replace("&amp", "");
sample = sample.replace("=", " ");
//
// tokenize the String Object
String[] token = sample.split(";");
//
// Loop and extract the data into vars
for (i = 0; i < token.length; i++){
if (trace)
System.out.println("Queued (length, value) " +
token.length() + " "+ token);
if (token.length()> 3 &
token.substring(0, 4).equals("City")){
city = token.substring(4).trim();
} else if (token.length()> 4 &
token.substring(0, 5).equals("Email")){
email = token.substring(5).trim();
} else if (token.length()> 4 &
token.substring(0, 5).equals("State")){
state = token.substring(5).trim();
} else if (token.length()> 6 &
token.substring(0, 7).equals("Country")){
country = token.substring(7).trim();
} else if (token.length()> 7 &
token.substring(0, 8).equals("LastName")){
lastName = token.substring(8).trim();
} else if (token.length()>7 &
token.substring(0, 8).equals("FullName")){
fullName = token.substring(8).trim();
fullName = fullName.replace("+", " ");
} else if (token.length()> 8 &
token.substring(0, 9).equals("FirstName")){
firstName = token.substring(9).trim();
} else if (token.length() > 9 &
(token.substring(0, 10).equals("CountryCode"))) {
countryCode = token.substring(11).trim();
} else
if (trace)
System.out.println("Not Used: " + token);
}
//
// Just display them
System.out.println("firstName: " + firstName);
System.out.println("lastName: " + lastName);
System.out.println("fullName: " + fullName);
System.out.println("city: " + city);
System.out.println("state: " + state);
System.out.println("country: " + country);
System.out.println("Country Code: " + state);
System.out.println("Email: " + email);
}
}
 
I

IchBin

Mark said:
Hmm, that's a good question. I assumed the answer would be "use
java.net.URL" but there's no way to parse the query string. You can use
getQuery() to get the whole thing, which seems a bit safer than your
method.

You can look at this code. It has a trace option at the top. I set it to
true. It will do what you want but I am not happy with it. I think I can
clean it up and make it tighter. Anyway, here ya go. This may get you
started in the right direction.

Here is test output with trace of the data you submitted in you message:
passing.

Queued (length, value) 65 <a
href=http://email.people.yahoo.com/py/psEmailVcard.vcf?Pyt=Tps
Not Used: <a href=http://email.people.yahoo.com/py/psEmailVcard.vcf?Pyt=Tps
Queued (length, value) 10 srch=email
Not Used: srch=email
Queued (length, value) 13 ext=vcard.vcf
Not Used: ext=vcard.vcf
Queued (length, value) 16 FirstName=George
Queued (length, value) 13 LastName=Bush
Queued (length, value) 20 FullName=George+Bush
Queued (length, value) 15 City=Washington
Queued (length, value) 8 State=DC
Queued (length, value) 10 Country=US
Queued (length, value) 14 CountryCode=US
Queued (length, value) 26 [email protected]
Queued (length, value) 14 URL=>vCard</a>
Not Used: URL=>vCard</a>
FirstName: George
LastName: Bush
FullName: George Bush
City: Washington
State: DC
Country: US
Country Code: US
Email: (e-mail address removed)


Here is the code:

public class Parsetest
{
// Set to display some trace information
static boolean trace = true;

public static void main(String[] args)
{
int i = 0;
String sample = "<a href="
+
"http://email.people.yahoo.com/py/ps...de=US&amp;[email protected]&amp;URL="
+ ">vCard</a>";
String firstName = "", lastName = "", fullName = "";
String city = "", state = "", country = "", countryCode = "",
email = "";
//
// Delete some inital stuff
sample = sample.replace("&amp", "");
// sample = sample.replace("=", " ");
//
// tokenize the String Object
String[] token = sample.split(";");
//
// Loop and extract the data into vars
for (i = 0; i < token.length; i++){
if (trace)
System.out.println("Queued (length, value) " +
token.length() + " "+ token);
if (token.length()> 4 &
token.substring(0, 5).equals("City=")){
city = token.substring(5);
} else if (token.length()> 5 &
token.substring(0, 6).equals("Email=")){
email = token.substring(6);
} else if (token.length()> 5 &
token.substring(0, 6).equals("State=")){
state = token.substring(6);
} else if (token.length()> 7 &
token.substring(0, 8).equals("Country=")){
country = token.substring(8);
} else if (token.length()> 8 &
token.substring(0, 9).equals("LastName=")){
lastName = token.substring(9);
} else if (token.length()>8 &
token.substring(0, 9).equals("FullName=")){
fullName = token.substring(9);
fullName = fullName.replace("+", " ");
} else if (token.length()> 9 &
token.substring(0, 10).equals("FirstName=")){
firstName = token.substring(10);
} else if (token.length() > 11 &
(token.substring(0, 8).equals("CountryC"))) {
countryCode = token.substring(12);
} else
if (trace)
System.out.println("Not Used: " + token);
}
//
// Just display them
System.out.println("FirstName: " + firstName);
System.out.println("LastName: " + lastName);
System.out.println("FullName: " + fullName);
System.out.println("City: " + city);
System.out.println("State: " + state);
System.out.println("Country: " + country);
System.out.println("Country Code: " + countryCode);
System.out.println("Email: " + email);
}
}
 
I

IchBin

Mark said:
Hmm, that's a good question. I assumed the answer would be "use
java.net.URL" but there's no way to parse the query string. You can use
getQuery() to get the whole thing, which seems a bit safer than your
method.

You can look at this code. It has a trace option at the top. I set it to
true. It will do what you want but I am not happy with it. I think I can
clean it up and make it tighter. Anyway, here ya go. This may get you
started in the right direction.

Here is test output with trace of the data you submitted in you message:
passing.

Queued (length, value) 65 <a
href=http://email.people.yahoo.com/py/psEmailVcard.vcf?Pyt=Tps
Not Used: <a href=http://email.people.yahoo.com/py/psEmailVcard.vcf?Pyt=Tps
Queued (length, value) 10 srch=email
Not Used: srch=email
Queued (length, value) 13 ext=vcard.vcf
Not Used: ext=vcard.vcf
Queued (length, value) 16 FirstName=George
Queued (length, value) 13 LastName=Bush
Queued (length, value) 20 FullName=George+Bush
Queued (length, value) 15 City=Washington
Queued (length, value) 8 State=DC
Queued (length, value) 10 Country=US
Queued (length, value) 14 CountryCode=US
Queued (length, value) 26 [email protected]
Queued (length, value) 14 URL=>vCard</a>
Not Used: URL=>vCard</a>
FirstName: George
LastName: Bush
FullName: George Bush
City: Washington
State: DC
Country: US
Country Code: US
Email: (e-mail address removed)


Here is the code:

public class Parsetest
{
// Set to display some trace information
static boolean trace = true;

public static void main(String[] args)
{
int i = 0;
String sample = "<a href="
+
"http://email.people.yahoo.com/py/ps...de=US&amp;[email protected]&amp;URL="
+ ">vCard</a>";
String firstName = "", lastName = "", fullName = "";
String city = "", state = "", country = "", countryCode = "",
email = "";
//
// Delete some inital stuff
sample = sample.replace("&amp", "");
//
// tokenize the String Object
String[] token = sample.split(";");
//
// Loop and extract the data into vars
for (i = 0; i < token.length; i++){
if (trace)
System.out.println("Queued (length, value) " +
token.length() + " "+ token);
if (token.length()> 4 &
token.substring(0, 5).equals("City=")){
city = token.substring(5);
} else if (token.length()> 5 &
token.substring(0, 6).equals("Email=")){
email = token.substring(6);
} else if (token.length()> 5 &
token.substring(0, 6).equals("State=")){
state = token.substring(6);
} else if (token.length()> 7 &
token.substring(0, 8).equals("Country=")){
country = token.substring(8);
} else if (token.length()> 8 &
token.substring(0, 9).equals("LastName=")){
lastName = token.substring(9);
} else if (token.length()>8 &
token.substring(0, 9).equals("FullName=")){
fullName = token.substring(9);
fullName = fullName.replace("+", " ");
} else if (token.length()> 9 &
token.substring(0, 10).equals("FirstName=")){
firstName = token.substring(10);
} else if (token.length() > 11 &
(token.substring(0, 8).equals("CountryC"))) {
countryCode = token.substring(12);
} else
if (trace)
System.out.println("Not Used: " + token);
}
//
// Just display them
System.out.println("FirstName: " + firstName);
System.out.println("LastName: " + lastName);
System.out.println("FullName: " + fullName);
System.out.println("City: " + city);
System.out.println("State: " + state);
System.out.println("Country: " + country);
System.out.println("Country Code: " + countryCode);
System.out.println("Email: " + email);
}
}
 
A

Andrew Thompson

Arne said:
Like "Spiderman" ??

:)

That works for ..me.

But I realised after I made that post that what is
'obviously false' to one person, might not be obvious
to another (especially someone from a different culture) -
and my idea was inherently flawed because of that.

(sighs) ..oh well.

Andrew T.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top