Six Stars

[resolved] Getting the occurrence position of string or charact in a given string

Hi,
I have column with the data like 123~456~12345~skjdh~hjg~fhjd~dghjuy~ether~SDFH~sdghtk~louiob  now in this data i want to know the index of nth occurrence of character "~" if i say 4th occurrence it should return 20 to me,  if i say 2nd occurrence it should return 8 to me, any Idea how to achieve this
Thanks in advance
1 ACCEPTED SOLUTION

Accepted Solutions
Four Stars

Re: [resolved] Getting the occurrence position of string or charact in a given string

Hi,
You can modify the code recommended by laurent...
In tJava i tested for 
String foo = "123~456~12345~skjdh~hjg~fhjd~dghjuy~ether~SDFH~sdghtk~louiob";
System.out.println(getIndex.functionxyz(foo,"~"));
Create a routine to handle this: (need modification as per your requirement or logic)
package routines;
public class getIndex {
public static int functionxyz(String message, String delimiter) {
int index = message.indexOf(delimiter);
while (index >= 0) {
   System.out.println(index);
   index = message.indexOf(delimiter, index + 1);
if (index == 7)
{
System.out.println("This is Second return for the 7th position " + 15);
}
}
return index;
}
}
Hope it helps.
Thanks
Vaibhav
8 REPLIES
Four Stars

Re: [resolved] Getting the occurrence position of string or charact in a given string

Hi ashokvv,
If the data is coming in the column... could not understand the concept of "i say".. can you elaborate with expected output for current data and one more sample data?
Vaibhav
Seventeen Stars

Re: [resolved] Getting the occurrence position of string or charact in a given string

hi, 
have a look at : https://stackoverflow.com/questions/5034442/java-indexes-of-all-occurrences-of-character-in-a-string
hope it help
regards
laurent
Six Stars

Re: [resolved] Getting the occurrence position of string or charact in a given string

Hi Vaibhav
I have column with the data like 123~456~12345~skjdh~hjg~fhjd~dghjuy~ether~SDFH~sdghtk~louiob  now in this data i want to know the index of nth occurrence of  specific character 
I was expecting this functionality through a built in function.
functionxyz(column1,"~",4) should return 20 
functionxyz(column1,"~",2) should return 8
functionxyz(column1,"4",2) should return 12
functionxyz(column1,"1",2) should return 9
Like Wise..
Six Stars

Re: [resolved] Getting the occurrence position of string or charact in a given string

Hi ashokvv,
If the data is coming in the column... could not understand the concept of "i say".. can you elaborate with expected output for current data and one more sample data?
Vaibhav

Hi Vaibhav
I have column with the data like 123~456~12345~skjdh~hjg~fhjd~dghjuy~ether~SDFH~sdghtk~louiob  now in this data i want to know the index of nth occurrence of  specific character 
I was expecting this functionality through a built in function.
functionxyz(column1,"~",4) should return 20 
functionxyz(column1,"~",2) should return 8
functionxyz(column1,"4",2) should return 12
functionxyz(column1,"1",2) should return 9
Like Wise..
Four Stars

Re: [resolved] Getting the occurrence position of string or charact in a given string

Hi,
You can modify the code recommended by laurent...
In tJava i tested for 
String foo = "123~456~12345~skjdh~hjg~fhjd~dghjuy~ether~SDFH~sdghtk~louiob";
System.out.println(getIndex.functionxyz(foo,"~"));
Create a routine to handle this: (need modification as per your requirement or logic)
package routines;
public class getIndex {
public static int functionxyz(String message, String delimiter) {
int index = message.indexOf(delimiter);
while (index >= 0) {
   System.out.println(index);
   index = message.indexOf(delimiter, index + 1);
if (index == 7)
{
System.out.println("This is Second return for the 7th position " + 15);
}
}
return index;
}
}
Hope it helps.
Thanks
Vaibhav
Seventeen Stars

Re: [resolved] Getting the occurrence position of string or charact in a given string

thanks sanvaibhav Smiley Wink
Six Stars

Re: [resolved] Getting the occurrence position of string or charact in a given string

Hi sanvaibhav 
Thanks for you input
I have done this some modifications in the code and is working fine
package routines;
public class Customfunctions {
public static int Occurrenceindex(String string, String letter, int i) {
if (string == null|| letter == null ||  i == 0){
    return 0;
    } else {
int index = string.indexOf(letter);
while (i > 1) {
index = string.indexOf(letter, index + 1);
i=i-1;
}
return index;
}
}
}
Regards
Ashok
Four Stars

Re: [resolved] Getting the occurrence position of string or charact in a given string

Hi Ashok,
Happy that you could re-use the code and implement it for your need.
Thanks for posting updated code.
Vaibhav