0

I have this code:

if (TextParsingConfiguration.join == true)
                {
                    images = images.Trim(' ');
                }
                if (TextParsingConfiguration.removecommas == true)
                {
                    images = ReplacingChars(images, new[] { ',', '"' }, " ");
                }

join and removecommas are bool variables im using them also in form1 in checkBoxes. And the ReplacingChars method:

public static string ReplacingChars(string source, char[] toReplace, string withThis)
        {
            return string.Join(withThis, source.Split(toReplace, StringSplitOptions.None));
        }

Maybe im wrong here with the logic but i wanted to give the user two options.

  1. to remove all Remove commas and Quotation marks this the removecommas bool variable.
  2. to join meaning to remove all the whit spaces but not the commas and Quotation marks just remove/delete the white spaces so the whole text will be like a block of text.

The question if number 2 is logic at all ? And if not what other options to remove(clean) i can make ? The removecommas is working. Its removing commas and Quotation marks and leave the spaces as it was.

Doron Muzar
  • 443
  • 1
  • 10
  • 26

4 Answers4

9

Try this :

   images= images.Replace(" ", String.Empty);
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
Monika
  • 2,172
  • 15
  • 24
  • The question talks about whitespace, not just space. Whitespace includes `\t`, `\r`, and `\n` also - to name but 3. – Moo-Juice Nov 26 '13 at 09:42
  • 1
    Your answer before the diting was working. Then its my mistake i meant to remove any spaces like you did in the first solution. But this maybe i can add a third option to remove only white spaces and not all the spaces ? – Doron Muzar Nov 26 '13 at 09:44
  • What is the diffrenece between your solution now and one with the \S+ im getting the same result on both of them ? – Doron Muzar Nov 26 '13 at 09:47
  • @DoronMuzar I think this is the faster way then using regex. – Monika Nov 26 '13 at 09:48
  • The regular expression class `\s` contains all white space, not just a space character. – Thorsten Dittmar Nov 26 '13 at 09:49
  • Regex.Replace(images, @"\s+", string.Empty) will remove all whitespaces. – Robert Smith May 13 '21 at 12:37
5

You could use Regex.Replace like this:

string newString = Regex.Replace(sourceString, @"\s+", replacement);
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
2

Maybe something like this:

images = Regex.Replace(images, @"\s+", "");
Oleksii Aza
  • 5,368
  • 28
  • 35
2

You can split the text like below,

 string[] sp = new string[] { " ", "\t", "\r" };

            string[] aa = images.Split(sp, StringSplitOptions.RemoveEmptyEntries);
Libyssa
  • 51
  • 1
  • 5