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

Recommended Answers

All 11 Replies

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

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

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!

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 .

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.

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

output=ccd

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

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

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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.