954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Dividing a string

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

Rentro
Newbie Poster
6 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

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

specifically at the substring method

jerbo
Junior Poster in Training
84 posts since Sep 2004
Reputation Points: 11
Solved Threads: 1
 

you can also use the indexOf method of the string class ... see the String APIs for more reference.

nanosani
Unauthenticated Liar
Team Colleague
1,830 posts since Jul 2004
Reputation Points: 45
Solved Threads: 56
 

SUBSTRING :D That was the word I was looking for. Thanks a lot guys .

Rentro
Newbie Poster
6 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

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!

conmuacuoi
Newbie Poster
4 posts since Sep 2004
Reputation Points: 10
Solved Threads: 1
 

Rentro,

You it it right on. Java actually has a substring function.

String 	substring(int beginIndex, int endIndex)
          // Returns a new string that is a substring of this string.

See the jdocs for details .


Ed

SUBSTRING :D That was the word I was looking for. Thanks a lot guys .
cosi
Junior Poster
153 posts since Aug 2004
Reputation Points: 17
Solved Threads: 1
 

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.

Parsu7
Light Poster
29 posts since Feb 2008
Reputation Points: 9
Solved Threads: 1
 

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

output=ccd

Parsu7
Light Poster
29 posts since Feb 2008
Reputation Points: 9
Solved Threads: 1
 

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

int pos = firstString.indexOf("ee");
System.out.println(firstString.substring(pos, pos+2));

stultuske
Posting Sensei
3,135 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

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.

Parsu7
Light Poster
29 posts since Feb 2008
Reputation Points: 9
Solved Threads: 1
 

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.

String originalString = "original String";
String toFind = "rig"; // can be entered by user
int pos =  originalString.indexOf(toFind);
System.out.println(originalString.substring(pos, pos + toFind.length());

toFind = "St"; // new String chosen by user
pos = originalString.indexOf(toFind);
System.out.println(originalString.substring(pos, pos + toFind.length());


use it like that, and you can change both the 'originalString' as the 'toFind', without your code becoming useless.

stultuske
Posting Sensei
3,135 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 
String originalString = "original String";
String toFind = "rig"; // can be entered by user
int pos =  originalString.indexOf(toFind);
System.out.println(originalString.substring(pos, pos + toFind.length());

toFind = "St"; // new String chosen by user
pos = originalString.indexOf(toFind);
System.out.println(originalString.substring(pos, pos + toFind.length());

use it like that, and you can change both the 'originalString' as the 'toFind', without your code becoming useless.


thanks for this syntax.

Parsu7
Light Poster
29 posts since Feb 2008
Reputation Points: 9
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You