how can I replace space with other signshttp://forums.asp.net/t/1799464.aspx/1?how+can+I+replace+space+with+other+signsThu, 03 May 2012 14:43:15 -040017994644962780http://forums.asp.net/p/1799464/4962780.aspx/1?how+can+I+replace+space+with+other+signshow can I replace space with other signs <p>Hi, I am trying to convert space in a string into other characters. For example: input: &quot;Mr John Smith&quot; ouput &quot;Mr%John%Smith&quot;. We need StringBuilder ? Please advise. </p> <p></p> 2012-05-03T02:09:35-04:004962805http://forums.asp.net/p/1799464/4962805.aspx/1?Re+how+can+I+replace+space+with+other+signsRe: how can I replace space with other signs <p>Sorry, I didn't make it clear. the string could contain many space, like &quot;Mr&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; John&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Smith&quot;, how to convert those space to &quot;%&quot; ?&nbsp;</p> 2012-05-03T02:51:08-04:004962992http://forums.asp.net/p/1799464/4962992.aspx/1?Re+how+can+I+replace+space+with+other+signsRe: how can I replace space with other signs <p>@ <a title="sdnd2000" href="http://forums.asp.net/members/sdnd2000.aspx">sdnd2000</a></p> <p>TIMTOWTDI&nbsp; =.&nbsp; there is more than one way to do it</p> <p>FWIW, StringBuilder is not necessary, although this example which i've tested in LINQPad v4.42.01 does use StringBuilder:&nbsp;</p> <pre class="prettyprint">public static String spacesBeGone(Char placeHolder, String stringToCompact) { List&lt;Char&gt; compactedStringProxy = new List&lt;Char&gt;(); Boolean compressingSpaces = true; foreach (Char thisChar in stringToCompact) { switch (thisChar) { case ' ': if (!compressingSpaces) { compressingSpaces = true; compactedStringProxy.Add(placeHolder); } break; default: compressingSpaces = false; compactedStringProxy.Add(thisChar); break; } } StringBuilder compactedString = new StringBuilder(); foreach (Char proxyChar in compactedStringProxy) { compactedString.Append(proxyChar); } return compactedString.ToString(); }</pre> <p>test</p> <pre class="prettyprint">void Main() { Console.WriteLine (spacesBeGone('%', "Mr John Smith")); Console.WriteLine (spacesBeGone('%', "Mr John Smith")); Console.WriteLine (spacesBeGone('&iquest;', @"Hi, I am trying to convert space in a string into other characters. For example: input: ""Mr John Smith"" ouput ""Mr%John%Smith"". We need StringBuilder ? Please advise.")); }</pre> <p>test output</p> <pre class="prettyprint">Mr%John%Smith Mr%John%Smith Hi,&iquest;I&iquest;am&iquest;trying&iquest;to&iquest;convert&iquest;space&iquest;in&iquest;a&iquest;string&iquest;into&iquest;other&iquest;characters.&iquest;For&iquest;example:&iquest;input:&iquest;"Mr&iquest;John&iquest;Smith"&iquest;ouput&iquest;"Mr%John%Smith".&iquest;We&iquest;need&iquest;StringBuilder&iquest;?&iquest;Please&iquest;advise.</pre> <p>g.<br> <br> <br> </p> 2012-05-03T05:31:43-04:004963013http://forums.asp.net/p/1799464/4963013.aspx/1?Re+how+can+I+replace+space+with+other+signsRe: how can I replace space with other signs <p>@ <a title="sdnd2000" href="http://forums.asp.net/members/sdnd2000.aspx">sdnd2000</a></p> <p>oops, there is a BUG in my&nbsp;because i meant to test these boundary conditions and forgot:</p> <pre class="prettyprint">Console.WriteLine (spacesBeGone('*', &quot;&quot;)); Console.WriteLine (spacesBeGone('#', &quot; &quot;)); Console.WriteLine (spacesBeGone('^', &quot; Mr John Smith &quot;));</pre> <p>i.e., empty string, all spaces, leading and/or trailing spaces ... if you test with the previous version, you will see the BUG.</p> <p>revised code:</p> <pre class="prettyprint">public static String spacesBeGone(Char placeHolder, String stringToCompact) { List&lt;Char&gt; compactedStringProxy = new List&lt;Char&gt;(); Boolean compressingSpaces = true; Boolean thisIsStartOfString = true; // foreach (Char thisChar in stringToCompact) { switch (thisChar) { case ' ': if (!compressingSpaces) { compressingSpaces = true; compactedStringProxy.Add(placeHolder); } else if (thisIsStartOfString) { compressingSpaces = true; compactedStringProxy.Add(placeHolder); } break; default: compressingSpaces = false; compactedStringProxy.Add(thisChar); break; } thisIsStartOfString = false; } StringBuilder compactedString = new StringBuilder(); foreach (Char proxyChar in compactedStringProxy) { compactedString.Append(proxyChar); } return compactedString.ToString(); }</pre> <p>new test</p> <pre class="prettyprint">void Main() { Console.WriteLine (spacesBeGone('*', &quot;&quot;)); Console.WriteLine (spacesBeGone('#', &quot; &quot;)); Console.WriteLine (spacesBeGone('^', &quot; Mr John Smith &quot;)); Console.WriteLine (spacesBeGone('%', "Mr John Smith")); Console.WriteLine (spacesBeGone('%', "Mr John Smith")); Console.WriteLine (spacesBeGone('&iquest;', @"Hi, I am trying to convert space in a string into other characters. For example: input: ""Mr John Smith"" ouput ""Mr%John%Smith"". We need StringBuilder ? Please advise.")); }</pre> <p>new test output</p> <pre class="prettyprint"># ^Mr^John^Smith^ Mr%John%Smith Mr%John%Smith Hi,&iquest;I&iquest;am&iquest;trying&iquest;to&iquest;convert&iquest;space&iquest;in&iquest;a&iquest;string&iquest;into&iquest;other&iquest;characters.&iquest;For&iquest;example:&iquest;input:&iquest;"Mr&iquest;John&iquest;Smith"&iquest;ouput&iquest;"Mr%John%Smith".&iquest;We&iquest;need&iquest;StringBuilder&iquest;?&iquest;Please&iquest;advise.</pre> <p>g.</p> 2012-05-03T05:46:41-04:004963027http://forums.asp.net/p/1799464/4963027.aspx/1?Re+how+can+I+replace+space+with+other+signsRe: how can I replace space with other signs <p>@ <a title="sdnd2000" href="http://forums.asp.net/members/sdnd2000.aspx">sdnd2000</a></p> <p>note the test output from the previous example does not show the empty string that results from</p> <pre class="prettyprint">Console.WriteLine (spacesBeGone('*', &quot;&quot;));</pre> <p>this is not a BUG in my code ... imho, it's a bug in the forums.asp.net editor ....</p> <p>if put this in the Message editor's "insert code" window:<br />--------------------------------------------------------------<br /><br />#<br />^Mr^John^Smith^<br />Mr%John%Smith<br />Mr%John%Smith<br />Hi,&iquest;I&iquest;am&iquest;trying&iquest;to&iquest;convert&iquest;space&iquest;in&iquest;a&iquest;string&iquest;into&iquest;other&iquest;characters.&iquest;For&iquest;example:&iquest;input:&iquest;"Mr&iquest;John&iquest;Smith"&iquest;ouput&iquest;"Mr%John%Smith".&iquest;We&iquest;need&iquest;StringBuilder&iquest;?&iquest;Please&iquest;advise.<br />--------------------------------------------------------------</p> <p>and i get this back</p> <pre class="prettyprint"># ^Mr^John^Smith^ Mr%John%Smith Mr%John%Smith Hi,&iquest;I&iquest;am&iquest;trying&iquest;to&iquest;convert&iquest;space&iquest;in&iquest;a&iquest;string&iquest;into&iquest;other&iquest;characters.&iquest;For&iquest;example:&iquest;input:&iquest;"Mr&iquest;John&iquest;Smith"&iquest;ouput&iquest;"Mr%John%Smith".&iquest;We&iquest;need&iquest;StringBuilder&iquest;?&iquest;Please&iquest;advise. </pre> <p>g.</p> 2012-05-03T05:56:43-04:004963298http://forums.asp.net/p/1799464/4963298.aspx/1?Re+how+can+I+replace+space+with+other+signsRe: how can I replace space with other signs <p>var result = inStr.Replace(' ', '%');</p> 2012-05-03T08:50:31-04:004963324http://forums.asp.net/p/1799464/4963324.aspx/1?Re+how+can+I+replace+space+with+other+signsRe: how can I replace space with other signs <p>@ <a title="Paul Linton" href="http://forums.asp.net/members/Paul%20Linton.aspx"> Paul Linton</a></p> <p>Hi Paul ... sorry,&nbsp;your suggestion&nbsp;does not work, because,&nbsp;... if you read the O.P.'s followup, you'll see that the problem is to replace 1 to n spaces with a single character.</p> <p>g.</p> 2012-05-03T09:00:59-04:004963451http://forums.asp.net/p/1799464/4963451.aspx/1?Re+how+can+I+replace+space+with+other+signsRe: how can I replace space with other signs <p>You can use regex</p> <pre class="prettyprint">string testdata = &quot;Mr John Smith&quot;; testdata = System.Text.RegularExpressions.Regex.Replace(testdata, @&quot;\s&#43;&quot;, &quot;%&quot;);</pre> <p><a href="http://msdn.microsoft.com/en-us/library/xwewhkd1.aspx">http://msdn.microsoft.com/en-us/library/xwewhkd1.aspx</a><br> <br> </p> 2012-05-03T10:02:09-04:004963563http://forums.asp.net/p/1799464/4963563.aspx/1?Re+how+can+I+replace+space+with+other+signsRe: how can I replace space with other signs <p></p> <blockquote><span class="icon-blockquote"></span> <h4>gerrylowry</h4> you'll see that the problem is to replace 1 to n spaces with a single character.</blockquote> <p></p> <p>It does not say that.&nbsp; It says 'space to &quot;%&quot;' it does not say 'space<strong>s</strong> to <strong>a single</strong>&nbsp;&quot;%&quot;'</p> 2012-05-03T10:54:10-04:004963875http://forums.asp.net/p/1799464/4963875.aspx/1?Re+how+can+I+replace+space+with+other+signsRe: how can I replace space with other signs <p>here I used LINQ, very easiest and might be space or space<strong>s</strong> :)</p> <pre class="prettyprint">string obj1 = &quot;Mr John Smith&quot;; string obj2 = &quot;Mr John Smith&quot;; string obj3 = &quot;MrJohn Smith&quot;; var result1 = string.Join(&quot;%&quot;, obj1.Split(' ').Where(T =&gt; T != &quot;&quot;).ToArray()); var result2 = string.Join(&quot;%&quot;, obj2.Split(' ').Where(T =&gt; T != &quot;&quot;).ToArray()); var result3 = string.Join(&quot;%&quot;, obj3.Split(' ').Where(T =&gt; T != &quot;&quot;).ToArray());</pre> <p><br> &nbsp;</p> 2012-05-03T13:39:15-04:004963908http://forums.asp.net/p/1799464/4963908.aspx/1?Re+how+can+I+replace+space+with+other+signsRe: how can I replace space with other signs <p>@ <a title="Paul Linton" href="http://forums.asp.net/members/Paul%20Linton.aspx"> Paul Linton</a></p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Paul Linton</h4> <p></p> <p>[the O.P. wrote] 'space to &quot;%&quot;'&nbsp;... not ... 'space<strong>s</strong> to <strong> a single</strong>&nbsp;&quot;%&quot;'</p> <p></p> </blockquote> <p></p> <p>Yes, Paul, but please look at the O.P.'s example:&nbsp; output &quot;Mr<span style="text-decoration:underline"><strong>%</strong></span>John<span style="text-decoration:underline"><strong>%</strong></span>Smith&quot;.&quot;</p> <p>i'm <em>guessing</em> the English is not <a title="sdnd2000" href="http://forums.asp.net/members/sdnd2000.aspx"> sdnd2000</a>'s mother tongue, examples:</p> <p style="padding-left:60px"><a href="http://forums.asp.net/post/4962780.aspx">http://forums.asp.net/post/4962780.aspx</a></p> <p style="padding-left:120px">I am trying to convert <strong>space</strong> in a string into other characters.</p> <p style="padding-left:120px">For example: input: &quot;Mr John Smith&quot; <strong>ou</strong><em>t</em><strong>put</strong> &quot;Mr%John%Smith&quot;.</p> <p style="padding-left:120px">[<em>Do</em>] we need StringBuilder ?</p> <p style="padding-left:60px"><a href="http://forums.asp.net/post/4962805.aspx">http://forums.asp.net/post/4962805.aspx</a>&nbsp;</p> &lt;div style=&quot;padding-left: 60px;&quot;&gt; <p style="padding-left:60px">the string could contain <span style="text-decoration:underline"> many</span> <strong>space</strong>, like &quot;Mr&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; John&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Smith&quot;,</p> <p style="padding-left:60px">how to convert <span style="text-decoration:underline"> those</span> <strong>space</strong> to &quot;%&quot; ?&nbsp;</p> &lt;/div&gt; <p>g.&nbsp;</p> 2012-05-03T13:58:45-04:004963931http://forums.asp.net/p/1799464/4963931.aspx/1?Re+how+can+I+replace+space+with+other+signsRe: how can I replace space with other signs <p>@ <a title="Mastan Oli" href="http://forums.asp.net/members/Mastan%20Oli.aspx"> Mastan Oli</a></p> <p>Hi Mastan ... your solution fails for the boundary conditions of leading and/or trailing spaces:</p> <pre class="prettyprint">string obj4 = &quot; Mr John Smith &quot;; string obj5 = &quot; Smith&quot;; string obj6 = &quot;MrJohn Smith &quot;; var result4 = string.Join(&quot;%&quot;, obj4.Split(' ').Where(T =&gt; T != &quot;&quot;).ToArray()); var result5 = string.Join(&quot;%&quot;, obj5.Split(' ').Where(T =&gt; T != &quot;&quot;).ToArray()); var result6 = string.Join(&quot;%&quot;, obj6.Split(' ').Where(T =&gt; T != &quot;&quot;).ToArray()); Console.WriteLine (result4); Console.WriteLine (result5); Console.WriteLine (result6);</pre> <p>output:</p> <pre class="prettyprint">Mr%John%Smith Smith MrJohn%Smith</pre> <p>output should be</p> <pre class="prettyprint">%Mr%John%Smith% %Smith MrJohn%Smith%</pre> <p>g,<br> <br> <br> </p> 2012-05-03T14:14:20-04:004963943http://forums.asp.net/p/1799464/4963943.aspx/1?Re+how+can+I+replace+space+with+other+signsRe: how can I replace space with other signs <p></p> <blockquote><span class="icon-blockquote"></span> <h4>gerrylowry</h4> Hi Mastan ... your solution fails for the boundary conditions of leading and/or trailing spaces:</blockquote> <p></p> <p>Just modify like</p> <pre class="prettyprint">const string strReplace = &quot;%&quot;; string obj4 = &quot; Mr John Smith &quot;; string obj5 = &quot; Smith&quot;; string obj6 = &quot;MrJohn Smith &quot;; var result4 = (obj4.StartsWith(&quot; &quot;) ? strReplace : &quot;&quot;) &#43; string.Join(&quot;%&quot;, obj4.Split(' ').Where(T =&gt; T != &quot;&quot;).ToArray()) &#43; (obj4.EndsWith(&quot; &quot;) ? strReplace : &quot;&quot;); var result5 = (obj5.StartsWith(&quot; &quot;) ? strReplace : &quot;&quot;) &#43; string.Join(&quot;%&quot;, obj5.Split(' ').Where(T =&gt; T != &quot;&quot;).ToArray()) &#43; (obj5.EndsWith(&quot; &quot;) ? strReplace : &quot;&quot;); var result6 = (obj6.StartsWith(&quot; &quot;) ? strReplace : &quot;&quot;) &#43; string.Join(&quot;%&quot;, obj6.Split(' ').Where(T =&gt; T != &quot;&quot;).ToArray()) &#43; (obj6.EndsWith(&quot; &quot;) ? strReplace : &quot;&quot;);</pre> <p></p> 2012-05-03T14:20:26-04:004963975http://forums.asp.net/p/1799464/4963975.aspx/1?Re+how+can+I+replace+space+with+other+signsRe: how can I replace space with other signs <p>@ <a title="mbanavige" href="http://forums.asp.net/members/mbanavige.aspx">mbanavige</a></p> <p>Hi Mike ... generally, i'm not a fan of regex; however, in this case, imho, your solution is <em>the most elegant</em> yet; your solution, by its nature, also handles the boundary conditions of empty string and leading/trailing spaces properly; plus, your regex expression <strong>\s&#43;</strong>&nbsp;requires only a minimal understanding of regex.</p> <p>regards, gerry</p> 2012-05-03T14:43:15-04:00