Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var pattern = @" +"; // match two or more spaces
- var groups = Regex.Split(input, pattern);
- var tokens = groups.Select(group => group.Replace(" ", String.Empty));
- var result = String.Join(' ', tokens.ToArray());
- // Replace all single whitespaces
- for (int i = 0; i < sourceString.Length; i++)
- {
- if (sourceString[i] = ' ')
- {
- if (i < sourceString.Length - 1 && sourceString[i+1] != ' ')
- sourceString = sourceString.Delete(i);
- }
- }
- // Replace multiple whitespaces
- while (sourceString.Contains(" ")) // Two spaces here!
- sourceString = sourceString.Replace(" ", " ");
- string str = "abc def ghi xyz";
- var result = str.Split(); //This will remove single spaces from the result
- StringBuilder sb = new StringBuilder();
- bool ifMultipleSpacesFound = false;
- for (int i = 0; i < result.Length;i++)
- {
- if (!String.IsNullOrWhiteSpace(result[i]))
- {
- sb.Append(result[i]);
- ifMultipleSpacesFound = false;
- }
- else
- {
- if (!ifMultipleSpacesFound)
- {
- ifMultipleSpacesFound = true;
- sb.Append(" ");
- }
- }
- }
- string output = sb.ToString();
- output = "abcdefghi xyz"
- public static string RemoveUnwantedSpaces(string text)
- {
- var sb = new StringBuilder();
- char lhs = '