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.

0

4 Answers 4

11

Try this :

   images= images.Replace(" ", String.Empty);
Sign up to request clarification or add additional context in comments.

6 Comments

The question talks about whitespace, not just space. Whitespace includes \t, \r, and \n also - to name but 3.
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 ?
What is the diffrenece between your solution now and one with the \S+ im getting the same result on both of them ?
@DoronMuzar I think this is the faster way then using regex.
The regular expression class \s contains all white space, not just a space character.
|
5

You could use Regex.Replace like this:

string newString = Regex.Replace(sourceString, @"\s+", replacement);

1 Comment

This should be the correct answer. For example: Regex.Replace(images, @"\s+", string.Empty)
2

Maybe something like this:

images = Regex.Replace(images, @"\s+", "");

1 Comment

Regex.Replace(images, @"\s+", string.Empty)
2

You can split the text like below,

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

            string[] aa = images.Split(sp, StringSplitOptions.RemoveEmptyEntries);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.