943,363 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5447
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 4th, 2004
0

Dividing a string

Expand Post »
I have a string that looks for example like this:

"aabbccddeeffgg"

I want to retreive the "ee" or the "cc" value from that string. How can I do that ? The tokenizer is not what I am looking for. I'm looking for the function that allows you to specify that you want the part of the string starting at letter 4 and ending at letter 7.

Please help ;')
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Rentro is offline Offline
6 posts
since Oct 2004
Oct 4th, 2004
0

Re: Dividing a string

Look at the String documentation at:
http://java.sun.com/j2se/1.4.2/docs/api/index.html

specifically at the substring method
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
jerbo is offline Offline
84 posts
since Sep 2004
Oct 4th, 2004
0

Re: Dividing a string

you can also use the indexOf method of the string class ... see the String APIs for more reference.
Team Colleague
Reputation Points: 45
Solved Threads: 56
Unauthenticated Liar
nanosani is offline Offline
1,767 posts
since Jul 2004
Oct 4th, 2004
0

Re: Dividing a string

SUBSTRING That was the word I was looking for. Thanks a lot guys .
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Rentro is offline Offline
6 posts
since Oct 2004
Oct 4th, 2004
0

Re: Dividing a string

Thank I used to them ,but I'm from to VietNam so I read English verry bad
can you give me some example.
Thank verry much!
Reputation Points: 10
Solved Threads: 1
Newbie Poster
conmuacuoi is offline Offline
4 posts
since Sep 2004
Oct 5th, 2004
0

Re: Dividing a string

Rentro,

You it it right on. Java actually has a substring function.
Java Syntax (Toggle Plain Text)
  1. String substring(int beginIndex, int endIndex)
  2. // Returns a new string that is a substring of this string.
See the jdocs for details.


Ed

Quote originally posted by Rentro ...
SUBSTRING That was the word I was looking for. Thanks a lot guys .
Reputation Points: 17
Solved Threads: 1
Junior Poster
cosi is offline Offline
153 posts
since Aug 2004
Feb 25th, 2008
0

Re: Dividing a string

Click to Expand / Collapse  Quote originally posted by Rentro ...
I have a string that looks for example like this:

"aabbccddeeffgg"

I want to retreive the "ee" or the "cc" value from that string. How can I do that ? The tokenizer is not what I am looking for. I'm looking for the function that allows you to specify that you want the part of the string starting at letter 4 and ending at letter 7.

Please help ;')
try using the following code it will surely work.

String s = "aabbccddeeffgg";
String s1 = s.substring(4,7);
System.out.println(s1);

it will give the output ccd.
Reputation Points: 9
Solved Threads: 1
Light Poster
Parsu7 is offline Offline
29 posts
since Feb 2008
Feb 25th, 2008
0

Re: Dividing a string

String s ="aabbccddeeffgg";
String s1=s.substring(4,7);
System.out.println(s1);

output=ccd
Reputation Points: 9
Solved Threads: 1
Light Poster
Parsu7 is offline Offline
29 posts
since Feb 2008
Feb 26th, 2008
0

Re: Dividing a string

but if it's "ee" you want as result:

int pos = firstString.indexOf("ee");
System.out.println(firstString.substring(pos, pos+2));
Reputation Points: 917
Solved Threads: 349
Nearly a Posting Maven
stultuske is offline Offline
2,453 posts
since Jan 2007
Feb 26th, 2008
0

Re: Dividing a string

Click to Expand / Collapse  Quote originally posted by stultuske ...
but if it's "ee" you want as result:

int pos = firstString.indexOf("ee");
System.out.println(firstString.substring(pos, pos+2));
if it is only "ee" then use

String s=firstString.substring(8,10);
System.out.println(s);

it will give the output "ee"
but if you would want any arbitary substring then your opinion is correct.
But then if you want an arbitary substring of three or more characters then it would not work, and you would be writing firstString.substring(pos,pos+3) or String.substring(pos,pos+4).

Anyway, thanks for sending me your opinion about this.
Reputation Points: 9
Solved Threads: 1
Light Poster
Parsu7 is offline Offline
29 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: replace string values with new one?
Next Thread in Java Forum Timeline: ClassNotFoundException with MySql and Netbeans





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC