title: How to ignore whitespaces and retain special characters only for Numeric
body: <p>I want to remove special characters from a string and need to retain special characters if it appears in-between, before or after any numeric value.</p>
  <p>For example,
  if my input is <code>&quot;My   14 &quot;  Apple -  laptop (note-book)  price is $ 900.45 but [I've paid only 900 $&quot;</code>
  then output should be <code>&quot;My 14 &quot; Apple laptop note-book price is $ 900.45 but I've paid only 900 $&quot;</code></p>
  <p>I removed extra whitespaces from a string using regex (<code>var res= str.replace(/^\s+|\s+$|\s+(?=\s)/g, &quot;&quot;);</code>) and now my sentence will have single whitespace between each word.</p>
  <p>In addition, I want to</p>
  <ol>
  <li><p>Remove all special characters which appears before and after string but should ignore if it appears in-between a string. <code>(ex: (note-book) will be note-book and I've will be I've)</code></p>
  </li>
  <li><p>Ignore set of specified special characters if it appears before, after or in-between a numeric.<code>(ex: $ 900.45 will be $ 900.45)</code></p>
  </li>
  </ol>
  <p>Initially I tried splitting the word using separator and implemented a logic to remove special characters based on numeric or word but it is not working as expected.</p>
  <pre><code>private static readonly string[] splChars = { &quot;#&quot;, &quot;₹&quot;, &quot;$&quot;, &quot;€&quot;, &quot;£&quot;, &quot;₽&quot;, &quot;¥&quot;, &quot;,&quot;, &quot;/&quot;, &quot;!&quot;, &quot;@&quot;, &quot;^&quot;, &quot;&amp;&quot;, &quot;*&quot;, &quot;(&quot;, &quot;)&quot;, &quot;'&quot;, &quot;\&quot;&quot;, &quot;;&quot;, &quot;_&quot;,&quot;-&quot;, &quot;:&quot;, &quot;|&quot;, &quot;[&quot;, &quot;]&quot;, &quot;{&quot;, &quot;}&quot; };
  char[] separators = { ' ' };
  private static readonly string[] splCharsNotAllowedForNumeric = { &quot;/&quot;, &quot;!&quot;, &quot;@&quot;, &quot;^&quot;, &quot;&amp;&quot;, &quot;(&quot;, &quot;)&quot;, &quot;'&quot;, &quot;\&quot;&quot;, &quot;;&quot;, &quot;_&quot;, &quot;|&quot;, &quot;[&quot;, &quot;]&quot;, &quot;{&quot;, &quot;}&quot; };
  string[] wrdList = res.Split(separators, StringSplitOptions.RemoveEmptyEntries);
  wrdList = RemoveSpecialCharacters(wrdList).ToArray();
  private static List&lt;string&gt; RemoveSpecialCharacters(string[] words)
  {
     List&lt;string&gt; cleaned = new List&lt;string&gt;();
     foreach (string word in words)
     {
         var split = word.Split(splChars, StringSplitOptions.RemoveEmptyEntries);
         var rejoined = string.Join(&quot;&quot;, split);
         if (IsNumeric(rejoined))
         {
             var numSplit = word.Split(splCharsNotAllowedForNumeric, StringSplitOptions.RemoveEmptyEntries);
             var numRejoined = string.Join(&quot;&quot;, numSplit);
             cleaned.Add(numRejoined);
         }
         else if (!string.IsNullOrWhiteSpace(split.FirstOrDefault()))
         {
             cleaned.Add(rejoined);
         }
     }
  
     return cleaned;  
  }
  </code></pre>
  <p>Can anyone suggest me a better solution to achieve this?</p>