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

Dani AI

Generated

Short answer: use Java’s String.substring, but be careful with indexes. Java strings are zero-based and substring(beginIndex, endIndex) includes the character at beginIndex and stops before endIndex (endIndex is exclusive). That means a human “letters 4..7” (1-based, inclusive) maps to substring(3, 7) in Java. See the official String API for details. (docs.oracle.com)
Java String API (substring & indexOf).

A safe helper that converts 1-based inclusive positions to a Java substring and validates bounds avoids off-by-one and exceptions:

static String slice1Based(String text, int startOneBased, int endOneBased) {
    if (text == null) return null;
    int start = Math.max(0, startOneBased - 1);
    int end = Math.min(text.length(), endOneBased);
    if (start >= end) return "";
    return text.substring(start, end);
}

This returns the characters 4..7 from "aabbccddeeffgg" as "bccd". Validate inputs to avoid StringIndexOutOfBoundsException (or a null). (docs.oracle.com)
StringIndexOutOfBoundsException doc.

When you need to locate a token (like "ee"), use indexOf but check the result before calling substring; and if you only need to compare text without allocating a new String, consider regionMatches to avoid creating substrings in tight loops. Always check pos != -1 and bounds before slicing. (docs.oracle.com)

A couple of production notes that matter later: Java’s substring works on UTF-16 char indexes (supplementary characters use two char positions), so use offsetByCodePoints / codePointCount when users mean “characters” rather than 16-bit code units. Also, substring used to share the original char[] (older JVMs) and could retain large buffers; modern OpenJDK/JVMs copy the range (changed around JDK 7u6), so long-running parsers should avoid lots of short-lived substrings by keeping index pointers or using a CharSequence wrapper if performance/memory is critical. (docs.oracle.com)

Notes tying back: and pointed to the docs correctly; and were right to suggest indexOf. The snippets above add safe bounds checking and a 1-based → 0-based conversion that avoids the common off-by-one pitfall seen in earlier replies.

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.