Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
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!
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;
}
row21.phone.replaceAll("\\d{2}\\D{2}", "")