revolutionkpi
revolutionkpi

Reputation: 2682

Remove white space from part of a string

I have the text:

SMS \r\n\t•    Map - locations of

How can I remove all of the white space between • and the first following character?

The above example should result in

SMS \r\n\t•Map - locations of

Upvotes: 0

Views: 148

Answers (3)

igofed
igofed

Reputation: 1442

You can use this Regex:

string toInsertBetween = string.Empty;
string toReplace = "SMS \r\n\t•    Map - locations of";
string res = Regex.Replace(toReplace, "•[ ]+([^ ])", "•" + toInsertBetween + "$1");

Upvotes: 0

Lukazoid
Lukazoid

Reputation: 19426

By using a regular expression it can be done like so:

var input = "SMS \r\n\t•    Map - locations of";

var regexPattern = @"(?<=•)\s+(?=\w)";
var cleanedInput = Regex.Replace(input, regexPattern, String.Empty);

This will replace any whitespace between • and the first word character with an empty string.

Upvotes: 3

daryal
daryal

Reputation: 14929

string s = "SMS \r\n\t•    Map - locations of";
string[] temp = s.Split('•');
s = temp[0]+temp[1].TrimStart(' ');

Upvotes: 1

Related Questions