-2

what I'm trying to do is I'm trying to reduce whitespace sizes to 1 characters (removing any unneeded whitespace. How should I deal with the task ?

PS.: NO Regex

EDIT.: Thanks, already succeeded, thanks for the split+join suggestion. Unfortunately, can't upvote any of the responses as frustrated teens have already -repspammed me for asking "a question too simple for the website"

EDIT2.: How do I make sure it doesn't remove a space in the front of the sentence, in case there is one ?

user2629770
  • 317
  • 2
  • 4
  • 8
  • 5
    Have you tried anything, or just requested solution here? Doing it with for loop is not a rocket science ... – Kamil Budziewski Jul 31 '13 at 08:45
  • split the text based on the text and then join all the strings if the string length is greater than one. – Mr_Green Jul 31 '13 at 08:48
  • I've tried going for sentence = sentence.Replace(" ", " "); but that's a terrible idea – user2629770 Jul 31 '13 at 08:50
  • how about `var res = string.Join(" ", myString.Split(new char []{' '} , StringSplitOptions.RemoveEmptyEntries));` – Damith Jul 31 '13 at 08:55
  • @Damith Will work, but I don't think that creating an array of strings and then rejoin them is very good for performance (expecially with long texts) – Steve Jul 31 '13 at 09:00

4 Answers4

5

What about

string.Join(" ", 
   myString.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))

Edit

As extension

    public static string RemoveWhiteSpaces(this string s)
    {
        return string.Join(" ", s.Split(new char[] { ' ' }, 
               StringSplitOptions.RemoveEmptyEntries));
    }
myString.RemoveWhiteSpaces();
Sayse
  • 42,633
  • 14
  • 77
  • 146
4

An extension for the string class

public static string RemoveExcessSpaces(this string str)
{
    StringBuilder sb = new StringBuilder(str.Length);
    bool first = true;
    for (int i = 0; i < str.Length; i++)
    {
        char c = str[i];
        switch (c)
        {
            case '\r':
            case '\n':
            case '\t':
            case ' ':
                if(first)
                {
                    sb.Append(c);
                    first = false;
                }
                else
                    continue;
                break;
            default:
                sb.Append(c);
                first = true;
                break;
        }
    }
    return sb.ToString();
}

Call it with

string result = "This is   a   test whit \t\t\r\n    multiple spaces";
result = result.RemoveExcessSpaces();

Giving credit to the original idea

Of course the set of chars that should be considered whitespaces could be defined passing a list of chars, (or using Char.IsWhiteSpace) but perhaps this could be better for the performance

Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
1

You may use this code:

public static String WhiteSpaceReducer(String value) {
  if (String.IsNullOrEmpty(value))
    return value;

  Boolean wasWhiteSpace = false;
  StringBuilder Sb = new StringBuilder();

  foreach (Char Ch in value) 
    if (Char.IsWhiteSpace(Ch)) {
      if (!wasWhiteSpace)
        Sb.Append(Ch);

      wasWhiteSpace = true;
    }
    else {
      wasWhiteSpace = false;
      Sb.Append(Ch);
    }

  return Sb.ToString();
}


...

String test = "   test   me  out  ";
String result = WhiteSpaceReducer(test);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

You do it like this

string myString = "I'm too            lazy for research.";
myString = Regex.Replace(myString, @"\s+", " ");
TheProvost
  • 1,832
  • 2
  • 16
  • 41