469,602 Members | 1,727 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

String Manipulation

I have 2 Java strings

1st String is a series of names, colons, and numbers
ie) Name1:13:Name2:4526:Name3:789:Name4:3729:Name5:6:N ame6:44

2nd String is a name
ie) Name2

I need to get the number associated with that name.

So I need a java function that traverses that first string, looks for the 2nd string, and returns the number that is directly after it. Thus in my example, I would pass the function String1 and String2 and it would return the number 4526.

Can someone help? My string manipulation skills in java are sub par...I know how to do it in sql, but not java.

-Jason
May 24 '07 #1
3 3268
JosAH
11,448 Expert 8TB
Have a look at the API documentation for the String.indexOf() method. A
bit of fiddling with it and the Integer.valueOf() method will do the job.

If you're feeling adventurous you can check out Java's regular expression support
too, but I'd recommend the first approach first.

kind regards,

Jos
May 24 '07 #2
prometheuzz
197 Expert 100+
I have 2 Java strings

1st String is a series of names, colons, and numbers
ie) Name1:13:Name2:4526:Name3:789:Name4:3729:Name5:6:N ame6:44

2nd String is a name
ie) Name2

I need to get the number associated with that name.

So I need a java function that traverses that first string, looks for the 2nd string, and returns the number that is directly after it. Thus in my example, I would pass the function String1 and String2 and it would return the number 4526.

Can someone help? My string manipulation skills in java are sub par...I know how to do it in sql, but not java.

-Jason
Have a look at the split(...) method from the String class: it returns an array of Strings. You can split it on the ":". Then your names will be on indexes 0,2,4,...,n and the associated numbers will be on 1,3,5,...,n+1. This of course assumes your names are nicely increasing every time!

Have a look at the API docs to see how the method works:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

Good luck.
May 24 '07 #3
if you know sql then u'll understand the Map interface in the java api. just look for a class that implements the interface, use the instantiate one of them and you're rolling.
May 25 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

32 posts views Thread by tshad | last post: by
29 posts views Thread by zoro | last post: by
4 posts views Thread by WaterWalk | last post: by
10 posts views Thread by micklee74 | last post: by
3 posts views Thread by crprajan | last post: by
22 posts views Thread by mann_mathann | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.