Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > substring() not working?

Reply
Thread Tools

substring() not working?

 
 
Peter Parker
Guest
Posts: n/a
 
      09-03-2006
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=email&amp;ext=vc ard.vcf&amp;FirstName=Bush&amp;LastName=George&amp ;FullName=George+Bush&amp;City=Washington&amp;Stat e=DC&amp;Country=US&amp;CountryCode=US&amp;Email=g &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"));.....


 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      09-03-2006
Peter Parker wrote:
> 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.

 
Reply With Quote
 
 
 
 
Peter Parker
Guest
Posts: n/a
 
      09-03-2006
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" <> wrote in message
news:M5CKg.227069$k%3.183842@dukeread12...
> 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;LastNa me=George&amp;FullName=Geo
rge+Bush&amp;City=Washington&amp;State=DC&amp;Coun try=US&amp;CountryCode=US&
amp;Email=&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"));.....
>
>



 
Reply With Quote
 
Ralf Seitner
Guest
Posts: n/a
 
      09-03-2006
Peter Parker schrieb:
> 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
 
Reply With Quote
 
Ralf Seitner
Guest
Posts: n/a
 
      09-03-2006
Peter Parker schrieb:
> 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=" ?
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      09-03-2006
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.

 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      09-03-2006
Peter Parker wrote:
> 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.

 
Reply With Quote
 
IchBin
Guest
Posts: n/a
 
      09-04-2006
Mark Space wrote:
> Peter Parker wrote:
>> 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.
>


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/psEmailVcard.vcf?Pyt=Tps&amp;srch=email&amp;ext=vc ard.vcf&amp;FirstName=George&amp;LastName=Bush&amp ;FullName=George+Bush&amp;City=Washington&amp;Stat e=DC&amp;Country=US&amp;CountryCode=US&amp;Email=g &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[i].length() + " "+ token[i]);

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

}
}
--
Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
Reply With Quote
 
IchBin
Guest
Posts: n/a
 
      09-04-2006
Mark Space wrote:
> Peter Parker wrote:
>> 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.
>


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
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:


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/psEmailVcard.vcf?Pyt=Tps&amp;srch=email&amp;ext=vc ard.vcf&amp;FirstName=George&amp;LastName=Bush&amp ;FullName=George+Bush&amp;City=Washington&amp;Stat e=DC&amp;Country=US&amp;CountryCode=US&amp;Email=g &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[i].length() + " "+ token[i]);
if (token[i].length()> 3 &
token[i].substring(0, 4).equals("City")){
city = token[i].substring(4).trim();
} else if (token[i].length()> 4 &
token[i].substring(0, 5).equals("Email")){
email = token[i].substring(5).trim();
} else if (token[i].length()> 4 &
token[i].substring(0, 5).equals("State")){
state = token[i].substring(5).trim();
} else if (token[i].length()> 6 &
token[i].substring(0, 7).equals("Country")){
country = token[i].substring(7).trim();
} else if (token[i].length()> 7 &
token[i].substring(0, .equals("LastName")){
lastName = token[i].substring(.trim();
} else if (token[i].length()>7 &
token[i].substring(0, .equals("FullName")){
fullName = token[i].substring(.trim();
fullName = fullName.replace("+", " ");
} else if (token[i].length()> 8 &
token[i].substring(0, 9).equals("FirstName")){
firstName = token[i].substring(9).trim();
} else if (token[i].length() > 9 &
(token[i].substring(0, 10).equals("CountryCode"))) {
countryCode = token[i].substring(11).trim();
} else
if (trace)
System.out.println("Not Used: " + token[i]);
}
//
// 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);
}
}

--
Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
Reply With Quote
 
IchBin
Guest
Posts: n/a
 
      09-04-2006
Mark Space wrote:
> Peter Parker wrote:
>> 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.
>


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=
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:


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/psEmailVcard.vcf?Pyt=Tps&amp;srch=email&amp;ext=vc ard.vcf&amp;FirstName=George&amp;LastName=Bush&amp ;FullName=George+Bush&amp;City=Washington&amp;Stat e=DC&amp;Country=US&amp;CountryCode=US&amp;Email=g &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[i].length() + " "+ token[i]);
if (token[i].length()> 4 &
token[i].substring(0, 5).equals("City=")){
city = token[i].substring(5);
} else if (token[i].length()> 5 &
token[i].substring(0, 6).equals("Email=")){
email = token[i].substring(6);
} else if (token[i].length()> 5 &
token[i].substring(0, 6).equals("State=")){
state = token[i].substring(6);
} else if (token[i].length()> 7 &
token[i].substring(0, .equals("Country=")){
country = token[i].substring(;
} else if (token[i].length()> 8 &
token[i].substring(0, 9).equals("LastName=")){
lastName = token[i].substring(9);
} else if (token[i].length()>8 &
token[i].substring(0, 9).equals("FullName=")){
fullName = token[i].substring(9);
fullName = fullName.replace("+", " ");
} else if (token[i].length()> 9 &
token[i].substring(0, 10).equals("FirstName=")){
firstName = token[i].substring(10);
} else if (token[i].length() > 11 &
(token[i].substring(0, .equals("CountryC"))) {
countryCode = token[i].substring(12);
} else
if (trace)
System.out.println("Not Used: " + token[i]);
}
//
// 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);
}
}

--
Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To be not, or not to be not? Ruby Freak Ruby 2 09-23-2008 08:04 AM
Why not 'foo = not f' instead of 'foo = (not f or 1) and 0'? Kristian Domke Python 11 01-23-2008 07:27 PM
'' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long. rote ASP .Net 2 01-23-2008 03:07 PM
Cisco 3640 3620 3600 not detecting, not enabling, not working: NM-2FE2W Taki Soho Cisco 0 09-22-2004 07:28 AM
maintaining control with cookies (not strictly an ASP or even server side question. But not not either) Stephanie Stowe ASP General 2 04-07-2004 04:23 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57