Skip to main content
Announcements
See what Drew Clarke has to say about the Qlik Talend Cloud launch! READ THE BLOG
cancel
Showing results for 
Search instead for 
Did you mean: 
devans_1
Creator
Creator

Locate first non occurrence of a character in a string

Hi Board,

Does anyone know how to find the first occurrence in a string that is not the one specified - e.g. I have a string

'ZZZZZZ1234'

and I would like to find the position of the first occurrence that is not 'Z' (i.e. '7'). There are lots of functions that allow locating a specific string but I can't find one that does the opposite. Note the above is just an example so I can't just look for '1' as this could be any character that isn't 'Z'.

2 Solutions

Accepted Solutions
rbartley
Specialist II
Specialist II

Hi,

 

How about using Replace(Word,'Z','') to get the string without the Zs, then take the length of the new field away from that of the original field, e.g.:

 

Word_TMP:

Load * Inline
[Word
'ZZZZZZ1234'];

Word:
Load *,
Len(Word)-Len(NoZs) as "FirstOccurenceNoZs";
Load
Word,
Replace(Word,'Z','') as "NoZs"
Resident Word_TMP;


Drop Table Word_TMP;

 

 

You could use the same approach directly in the chart, but it won't be as efficient since it will have to perform the operation in memory:

=Len(Word)-Len(Replace(Word,'Z',''))

View solution in original post

devans_1
Creator
Creator
Author

Thanks for this. I solved the problem, similar to yourself by using PurgeChar then looking for the occurrence of the first character left - e.g.

index (Patterna, left (PurgeChar (Patterna, '~'), 1))
where Patterna is a line like
'~~~~~~~1234'
so after a purge of '~' the first character is '1' and searching for this in the original string will give the position of this.

View solution in original post

2 Replies
rbartley
Specialist II
Specialist II

Hi,

 

How about using Replace(Word,'Z','') to get the string without the Zs, then take the length of the new field away from that of the original field, e.g.:

 

Word_TMP:

Load * Inline
[Word
'ZZZZZZ1234'];

Word:
Load *,
Len(Word)-Len(NoZs) as "FirstOccurenceNoZs";
Load
Word,
Replace(Word,'Z','') as "NoZs"
Resident Word_TMP;


Drop Table Word_TMP;

 

 

You could use the same approach directly in the chart, but it won't be as efficient since it will have to perform the operation in memory:

=Len(Word)-Len(Replace(Word,'Z',''))

devans_1
Creator
Creator
Author

Thanks for this. I solved the problem, similar to yourself by using PurgeChar then looking for the occurrence of the first character left - e.g.

index (Patterna, left (PurgeChar (Patterna, '~'), 1))
where Patterna is a line like
'~~~~~~~1234'
so after a purge of '~' the first character is '1' and searching for this in the original string will give the position of this.