I have a requirement to extract some data from big string
I get input as "Dial by :" from server and I need the input to be in format "Dial by:"
Input : Dial by :
Output Expected : Dial by:
can someone help me how to remove space b/w "y" and ":"
I have a requirement to extract some data from big string
I get input as "Dial by :" from server and I need the input to be in format "Dial by:"
Input : Dial by :
Output Expected : Dial by:
can someone help me how to remove space b/w "y" and ":"
You could use a regular expression to search for a optional whitespace followed by a :
at the end of a String
and replace it with a bare colon. Like,
String input = "Dial by :";
System.out.println(input.replaceAll("\\s*:$", ":"));
You could use String#replaceAll
here:
String input = "Dial by :";
String output = input.replaceAll("\\bDial by\\s+:", "Dial by:");
System.out.println(input + "\n" + output);
This prints:
Dial by :
Dial by:
If the string always starts with "Dial by", you could just do:
input = input.substring(0, 7) + input.substring(8);