Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
ashok5
Contributor III
Contributor III

[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
Labels (3)
1 Solution

Accepted Solutions
Anonymous
Not applicable

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

View solution in original post

9 Replies
Anonymous
Not applicable

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
Anonymous
Not applicable

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
ashok5
Contributor III
Contributor III
Author

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..
ashok5
Contributor III
Contributor III
Author

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..
Anonymous
Not applicable

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
Anonymous
Not applicable

thanks  sanvaibhav  0683p000009MA9p.png
ashok5
Contributor III
Contributor III
Author

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
Anonymous
Not applicable

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

Hi Team,

I need to implement this talend .. this is currently written in abinitio.Please help urgently

(is_defined(lookup("Lookup_File", ((decimal("\307"))re_index(id, "[a-zA-Z]+") - 1),string_substring(, (decimal("\307"))re_index(id, "[a-zA-Z]+") , string_length(id))).name))