Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

substring() not working?

Thread view: 
Peter Parker - 03 Sep 2006 15:58 GMT
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
=vcard.vcf&amp;FirstName=Bush&amp;LastName=George&amp;FullName=George+Bush&amp;C
ity=Washington&amp;State=DC&amp;Country=US&amp;CountryCode=US&amp;Email=gbush@wh
itehouse.com&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"));.....
Andrew Thompson - 03 Sep 2006 16:07 GMT
> 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.
Peter Parker - 03 Sep 2006 17:04 GMT
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.

> 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&
amp;Email=gbush@whitehouse.com&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"));.....
Ralf Seitner - 03 Sep 2006 17:16 GMT
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
[quoted text clipped - 13 lines]
>
> 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
Ralf Seitner - 03 Sep 2006 17:18 GMT
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
[quoted text clipped - 9 lines]
> String lastName = sample.substring(sample.indexOf("LastnameName=") +10,
> sample.indexOf("FullName="));
Perhaps "LastnameName=" is not found... isnt it "Lastname=" ?
Andrew Thompson - 03 Sep 2006 17:32 GMT
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.
Arne Vajhøj - 05 Sep 2006 16:30 GMT
> If you wish to use a false name, please make it an
> *obviously* false name

Like "Spiderman" ??

:-)

Arne
Andrew Thompson - 05 Sep 2006 16:51 GMT
> > If you wish to use a false name, please make it an
> > *obviously* false name
>
> 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.
Mark Space - 03 Sep 2006 23:54 GMT
> 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.
IchBin - 04 Sep 2006 07:11 GMT
>> 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
[quoted text clipped - 7 lines]
> 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
=vcard.vcf&amp;FirstName=George&amp;LastName=Bush&amp;FullName=George+Bush&amp;C
ity=Washington&amp;State=DC&amp;Country=US&amp;CountryCode=US&amp;Email=gbush@wh
itehouse.com&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, 8).equals("LastName")){
                lastName = token[i].substring(8).trim();
            } else if (token[i].length()>7 &
                       token[i].substring(0, 8).equals("FullName")){
                fullName = token[i].substring(8).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);

    }
}
Signature

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

IchBin - 04 Sep 2006 07:31 GMT
>> 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
[quoted text clipped - 7 lines]
> 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 gbush@whitehouse.com
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: gbush@whitehouse.com

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
=vcard.vcf&amp;FirstName=George&amp;LastName=Bush&amp;FullName=George+Bush&amp;C
ity=Washington&amp;State=DC&amp;Country=US&amp;CountryCode=US&amp;Email=gbush@wh
itehouse.com&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, 8).equals("LastName")){
                lastName = token[i].substring(8).trim();
            } else if (token[i].length()>7 &
                       token[i].substring(0, 8).equals("FullName")){
                fullName = token[i].substring(8).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);
     }
}

Signature

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

IchBin - 04 Sep 2006 09:18 GMT
>> 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
[quoted text clipped - 7 lines]
> 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=gbush@whitehouse.com
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: gbush@whitehouse.com

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
=vcard.vcf&amp;FirstName=George&amp;LastName=Bush&amp;FullName=George+Bush&amp;C
ity=Washington&amp;State=DC&amp;Country=US&amp;CountryCode=US&amp;Email=gbush@wh
itehouse.com&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, 8).equals("Country=")){
                country = token[i].substring(8);
            } 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, 8).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);
    }
}

Signature

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

IchBin - 04 Sep 2006 09:22 GMT
>> 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
[quoted text clipped - 7 lines]
> 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=gbush@whitehouse.com
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: gbush@whitehouse.com

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
=vcard.vcf&amp;FirstName=George&amp;LastName=Bush&amp;FullName=George+Bush&amp;C
ity=Washington&amp;State=DC&amp;Country=US&amp;CountryCode=US&amp;Email=gbush@wh
itehouse.com&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[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, 8).equals("Country=")){
                country = token[i].substring(8);
            } 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, 8).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);
    }
}

Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.