user2786737
user2786737

Reputation: 55

Find position of a substring in a string(not indexOf)

ex

String str = "abc def ghi";

Is there any method like str.find("abc") will return me 1 and str.find("def") will return me 2?

java language..

Upvotes: 3

Views: 14429

Answers (4)

Zachariah Rabatah
Zachariah Rabatah

Reputation: 312

If you are looking to find multiple positions of the same string try this code.

//Returns an array of integers with each string position
private int[] getStringPositions(String string, String subString){
        String[] splitString = string.split(subString); //Split the string

        int totalLen = 0; //Total length of the string, added in the loop

        int[] indexValues = new int[splitString.length - 1]; //Instances of subString in string

        //Loop through the splitString array to get each position
        for (int i = 0; i < splitString.length - 1; i++){

            if (i == 0) {
                //Only add the first splitString length because subString is cut here.
                totalLen = totalLen + splitString[i].length();
            }
            else{
                //Add totalLen & splitString len and subStringLen
                totalLen = totalLen + splitString[i].length() + subString.length();
            }

            indexValues[i] = totalLen; //Set indexValue at current i
        }

        return indexValues;
    }

So for example:

String string = "s an s an s an s"; //Length = 15
String subString = "an";

Answer would return indexValues = (2, 7, 12).

Upvotes: 1

Callum
Callum

Reputation: 879

I don't think there is a native function for this. But you could write your own.

It seems like you want to split the string based on the white space character.

String[] parts = string.split(" ");

Loop through the array created. And return the index + 1 (As java has zero based indexes)

for(int i = 0; i < parts.length; i++)
{
  if(parts[i].equals(parameter))
  {
     return i + 1;
  }
}

Upvotes: 1

MansoorShaikh
MansoorShaikh

Reputation: 923

Since you are not requesting for index of a substring but instead which word position the substring belong to, there is no such built-in method available. But you can split the input string by space character and read each item in the list returned by the split method and check at which list item position your substring belongs to.

Let me know, if you need code for this.

Upvotes: 1

Bernhard Barker
Bernhard Barker

Reputation: 55629

How about something like this?

int getIndex(String str, String substring)
{
  return Arrays.asList(str.split("\\s+")).indexOf(substring)+1;
}

Disclaimer: This isn't at all efficient. It splits the whole string from scratch every time the function is called.

Test code:

String str = "abc def ghi";
System.out.println(getIndex(str, "abc"));
System.out.println(getIndex(str, "def"));

Prints:

1
2

Explanation:

str.split("\\s+") splits the string by white-space and puts each part into a position in an array.

Arrays.asList returns an ArrayList for the array.

indexOf(substring) finds the position of the string in the ArrayList.

+1 since Java uses 0-indexing and you want 1-indexing.

Upvotes: 7

Related Questions