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

Announcements
Discover how organizations are unlocking new revenue streams: Watch here
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Removing characters after the first occurrence of a character

Hello Anyone,
How do I remove characters after the first occurrence of a character(in this case "/") in a string?
Input: 3456439/3456432
Output: 3456439
Thanks! 

Labels (2)
10 Replies
Anonymous
Not applicable
Author

This will do it.....

input.substring(0, input.indexOf("/"))

....it will fall over if the "/" is not in the string though. So make sure you prepare for that before running it
Anonymous
Not applicable
Author

Hi rhall_2.0,
can you check my expression, I can't make it work:
row2.Type.compareToIgnoreCase("Telephone numbers")== 0 ?
row3.Country_Code + "-" + row3.Area_Code + "-" + StringHandling.RIGHT(row2.Telephone,7) :
row2.Type.compareToIgnoreCase("Contains 2 numbers")== 0 ?
row2.Telephone.substring(0,row2.Telephone.indexOf("/"))
: "-"
Thanks rhall_2.0!
Anonymous
Not applicable
Author

It just came it my mind, I used StringHandling.LEFT(row2.Telephone,7)
TRF
Champion II
Champion II

row2.Telephone.replaceAll("/.*$", "")

should works.

Regards,
TRF
Anonymous
Not applicable
Author

Thanks TRF.
New question, I have data like this 4567890TO91. I already have "4567890" but how do I get "4567891" ?
Anonymous
Not applicable
Author

Are you wanting to do this.....
Start with 4567890TO91
Take the number before the "TO" to give you 4567890
Look at the number after the "TO" and replace the end part of the previous number with the number after the "TO" to give you 45678 + 91
If so, this method will do the job. You will need to create a Routine and put this code into the Routine. Then you can use wherever you need this functionality. I should say that I knocked this up quite quickly and there *may* be more efficient ways of doing this. However this should get you on the right track

    public static String[] MyMethod(String input, String separator){
String[] returnVal = new String[2];

//Check both input variables are not null or empty Strings
if(input!=null&&separator!=null&&input.compareToIgnoreCase("")!=0&&separator.compareToIgnoreCase("")!=0){
//Check the separator is in the input value
if(input.indexOf(separator)>-1){
String firstHalf = input.substring(0,input.indexOf(separator));
String afterSep = input.substring(input.indexOf(separator)+separator.length());

//Set the first String
returnVal[0] = firstHalf;

//Check the second String section is greater than 0 in length
if(afterSep.length()>0){
//Check that the afterSep section is greater than or equal to the firstHalf
if(firstHalf.length()>=afterSep.length()){
returnVal[1]=firstHalf.substring(0, firstHalf.length()-afterSep.length())+afterSep;
}else{
returnVal[1] = afterSep;
}
}
}
}

return returnVal;

}
TRF
Champion II
Champion II

Hi,
The transformation rule is not clear but shortly you can do it with regex:
row21.phone.replaceAll("\\d{2}\\D{2}", "") 


Regards,
TRF
Anonymous
Not applicable
Author

There is a difference between these two suggestions. If I read it correctly, TRF's solution will replace 2 digits followed by 2 chars with an empty String and leave you with....

45678 90TO 91 =  4567891
This will be far more efficient than what I gave.
The solution that I put together is a little more open to things like replacing the "TO" with any other String, with no value following the "TO" (of whatever) String, with no value preceeding that value and various lengths. For example, if you had.....

12TODAY99888



....the solution I put together would return 12 and 99888 (assuming that 99888 is bigger than 12 so therefore would replace the whole String). However, given what you gave it would produce 4567890 and 4567891 since the 91 is shorter than 4567890, so the number of chars that 91 takes up (2) is removed from 4567890 (45678) and the 91 is added.
The rules are these....
1) The separator value can be of any length greater than or equal to 1
2) The value after the separator value dictates how many characters are removed from the String before the separator value. So if the value after the separator value is 4 chars long,  then 4 chars will be removed from the right of the String before the separator value.
3) If the value after the separator value is greater in length than the value before the separator value, it completely overwrites the value before the separator value

Possibly a bit more than you wanted, but I saw it as a logical extrapolation of your example.


TRF
Champion II
Champion II

Honored to have a challenge with rhall_2.0    Smiley Wink
However, regex can be tuned for more flexibility, but as I said, we don't have expected rules.

Best regards,
TRF