简体   繁体   中英

How to get the (n) index of character inside String?

suppose you have string like:

String longStr="someText1@someText2@someText3@someText@someText4@someText5@someText6@someText7@someText8@someText9@someText10@someText11@someText12@someText13@someText14";

how to get the index of the 10th occurrence of the @ character in the above String ?

i need the index to split from that index till the end.

您可以使用StringUtils.ordinalIndexOf()来查找String中的第n个索引

String s=longStr.split("@")[9];

would give someText10

if you want the substring till the end

do

String sub=longStr.substring(indexOf(s));

or all in one:

String sub=longStr.substring(indexOf(longStr.split("@")[9]));

To get the index "manual" way (without StringUtils):

pos = -1;
for (int i = 0; i < 10; i++) {
    pos = longStr.indexOf('@', pos + 1);
}

To get the rest of the string without the index: use a regular expression. "(?:[^@]*@){10}(.*)" should do it.

Try this:

String longStr="someText1@someText2@someText3@someText@someText4@someText5@someText6@someText7@someText8@someText9@someText10@someText11@someText12@someText13@someText14";
System.out.println(longStr.substring(longStr.indexOf(longStr.split("@")[10])));

Output:

someText10@someText11@someText12@someText13@someText14

Use following code if you want to avoid ArrayIndexOutOfBoundsException , and you are not sure if string will contain 10 @ in it.

longStr.substring(longStr.indexOf(longStr.split("@")[Math.min(10,Math.max(longStr.lastIndexOf("@"),0))]))

What about this method:

public static int indexOf(String haystack, String needle, int ordinal) {
    try {
        return haystack.length() - haystack.split(needle, ordinal + 1)[ordinal].length() - 1;
    } catch (ArrayIndexOutOfBoundsException e) {
        return -1;
    }
}

And calling like:

String longStr="someText1@someText2@someText3@someText@someText4@someText5@someText6@someText7@someText8@someText9@someText10@someText11@someText12@someText13@someText14";
int idx = indexOf(longStr, "@", 10);
System.out.println("Index of 10th occurrence: " + idx);

Will output 98 .

testString.split("@", 11)[10]

This will return you required result. Method split(String regex, int limit) - This method splits the string using regex for number of limit times and returns array.

Hope, this will solve your problem.

Please try this:

public class StringUtils {

public static int findIndexOfCharacter(String str, char ch, int num){
    if(str == null || str.equals("")){
        return -1;
    }

    if(num <= 0 || num >= str.length() ){
        return -1;
    }

    if(!str.contains(String.valueOf(ch))){
        return -1;
    }else{
        int[] indexOfCh = new int[str.length()];
        int index = 0;
        String[] strArray = str.split("");
        for(int i = 1; i < str.length(); i++){
            if(strArray[i].charAt(0) == ch){
                indexOfCh[index] = i;
                index++;
            }
        }

        if(num > index){
            return -1;
        }
        return indexOfCh[num-1];
    }


}

}

JUnit Test:

@Test
public void testFindIndexOfCharacter(){
    longStr = "someText1@someText2@someText3@someText4@someText5@someText6@someText7@someText8@someText9@someText10@someText11@someText12@someText13@someText14@someText15";
    searchCh = '@';
    positionNum = 10;
    assertEquals(101, StringUtils.findIndexOfCharacter(longStr, searchCh, positionNum));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM