-2

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 ":"

mpb
  • 83
  • 1
  • 9
  • Will the leading text always be `Dial by`? Can you give more examples of how the replacement is supposed to happen? – Tim Biegeleisen May 15 '20 at 05:50
  • 1
    String will be always starts with "Dial by" – mpb May 15 '20 at 05:51
  • 1
    Does this answer your question? [replace String with another in java](https://stackoverflow.com/questions/5216272/replace-string-with-another-in-java) – Cray May 15 '20 at 10:02

3 Answers3

1

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*:$", ":"));
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

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:
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

If the string always starts with "Dial by", you could just do:

input = input.substring(0, 7) + input.substring(8);
Diana Shao
  • 61
  • 1
  • 8