Dividing a string

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2004
Posts: 6
Reputation: Rentro is an unknown quantity at this point 
Solved Threads: 0
Rentro Rentro is offline Offline
Newbie Poster

Dividing a string

 
0
  #1
Oct 4th, 2004
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 ;')
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 84
Reputation: jerbo is an unknown quantity at this point 
Solved Threads: 1
jerbo jerbo is offline Offline
Junior Poster in Training

Re: Dividing a string

 
0
  #2
Oct 4th, 2004
Look at the String documentation at:
http://java.sun.com/j2se/1.4.2/docs/api/index.html

specifically at the substring method
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 1,749
Reputation: nanosani is an unknown quantity at this point 
Solved Threads: 55
Team Colleague
nanosani's Avatar
nanosani nanosani is offline Offline
Unauthenticated Liar

Re: Dividing a string

 
0
  #3
Oct 4th, 2004
you can also use the indexOf method of the string class ... see the String APIs for more reference.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 6
Reputation: Rentro is an unknown quantity at this point 
Solved Threads: 0
Rentro Rentro is offline Offline
Newbie Poster

Re: Dividing a string

 
0
  #4
Oct 4th, 2004
SUBSTRING That was the word I was looking for. Thanks a lot guys .
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 4
Reputation: conmuacuoi is an unknown quantity at this point 
Solved Threads: 1
conmuacuoi's Avatar
conmuacuoi conmuacuoi is offline Offline
Newbie Poster

Re: Dividing a string

 
0
  #5
Oct 4th, 2004
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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 153
Reputation: cosi is an unknown quantity at this point 
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Re: Dividing a string

 
0
  #6
Oct 5th, 2004
Rentro,

You it it right on. Java actually has a substring function.
  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

Originally Posted by Rentro
SUBSTRING That was the word I was looking for. Thanks a lot guys .
In a world without walls or fences,
What use are Windows and Gates.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 28
Reputation: Parsu7 is an unknown quantity at this point 
Solved Threads: 1
Parsu7 Parsu7 is offline Offline
Light Poster

Re: Dividing a string

 
0
  #7
Feb 25th, 2008
Originally Posted by Rentro View 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 ;')
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 28
Reputation: Parsu7 is an unknown quantity at this point 
Solved Threads: 1
Parsu7 Parsu7 is offline Offline
Light Poster

Re: Dividing a string

 
0
  #8
Feb 25th, 2008
String s ="aabbccddeeffgg";
String s1=s.substring(4,7);
System.out.println(s1);

output=ccd
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Dividing a string

 
0
  #9
Feb 26th, 2008
but if it's "ee" you want as result:

int pos = firstString.indexOf("ee");
System.out.println(firstString.substring(pos, pos+2));
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 28
Reputation: Parsu7 is an unknown quantity at this point 
Solved Threads: 1
Parsu7 Parsu7 is offline Offline
Light Poster

Re: Dividing a string

 
0
  #10
Feb 26th, 2008
Originally Posted by stultuske View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 4120 | Replies: 11
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC