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

suppose you have a line 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 @ symbol in the above line?

I need an index to separate from this index to the end.

+3
source share
7 answers

You can use StringUtils.ordinalIndexOf () to search for the nth index inside a string

+7
source

To get the index manually (without StringUtils):

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

To get the rest of the string without an index: use regex. "(?:[^@]*@){10}(.*)"must do it.

+5
source

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

Text10

,

do

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

:

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

:

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

:

someText10@someText11@someText12@someText13@someText14

, ArrayIndexOutOfBoundsException, , 10 @.

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

How 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 a call 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.

+1
source

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

This will return you the desired result. Split method (String regex, int limit). This method breaks the string using a regular expression for the number of time limits and returns an array.

Hope this solves your problem.

0
source

Please try the following:

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));
}
0
source

All Articles