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.
FWIW, StringBuilder is not necessary, although this example which i've tested in LINQPad v4.42.01 does use StringBuilder:
public static String spacesBeGone(Char placeHolder, String stringToCompact)
{
List<Char> compactedStringProxy = new List<Char>();
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();
}
test
void Main()
{
Console.WriteLine (spacesBeGone('%', "Mr John Smith"));
Console.WriteLine (spacesBeGone('%', "Mr John Smith"));
Console.WriteLine (spacesBeGone('¿', @"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."));
}
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
oops, there is a BUG in my because i meant to test these boundary conditions and forgot:
Console.WriteLine (spacesBeGone('*', ""));
Console.WriteLine (spacesBeGone('#', " "));
Console.WriteLine (spacesBeGone('^', " Mr John Smith "));
i.e., empty string, all spaces, leading and/or trailing spaces ... if you test with the previous version, you will see the BUG.
revised code:
public static String spacesBeGone(Char placeHolder, String stringToCompact)
{
List<Char> compactedStringProxy = new List<Char>();
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();
}
new test
void Main()
{
Console.WriteLine (spacesBeGone('*', ""));
Console.WriteLine (spacesBeGone('#', " "));
Console.WriteLine (spacesBeGone('^', " Mr John Smith "));
Console.WriteLine (spacesBeGone('%', "Mr John Smith"));
Console.WriteLine (spacesBeGone('%', "Mr John Smith"));
Console.WriteLine (spacesBeGone('¿', @"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."));
}
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
Marked as answer by sdnd2000 on May 03, 2012 02:43 PM
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
Hi Paul ... sorry, your suggestion does not work, because, ... 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.
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
sdnd2000
Member
58 Points
111 Posts
how can I replace space with other signs
May 03, 2012 02:09 AM|LINK
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.
sdnd2000
Member
58 Points
111 Posts
Re: how can I replace space with other signs
May 03, 2012 02:51 AM|LINK
Sorry, I didn't make it clear. the string could contain many space, like "Mr John Smith", how to convert those space to "%" ?
gerrylowry
All-Star
20513 Points
5712 Posts
Re: how can I replace space with other signs
May 03, 2012 05:31 AM|LINK
@ sdnd2000
TIMTOWTDI =. there is more than one way to do it
FWIW, StringBuilder is not necessary, although this example which i've tested in LINQPad v4.42.01 does use StringBuilder:
public static String spacesBeGone(Char placeHolder, String stringToCompact) { List<Char> compactedStringProxy = new List<Char>(); 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(); }test
void Main() { Console.WriteLine (spacesBeGone('%', "Mr John Smith")); Console.WriteLine (spacesBeGone('%', "Mr John Smith")); Console.WriteLine (spacesBeGone('¿', @"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.")); }test output
g.
gerrylowry
All-Star
20513 Points
5712 Posts
Re: how can I replace space with other signs
May 03, 2012 05:46 AM|LINK
@ sdnd2000
oops, there is a BUG in my because i meant to test these boundary conditions and forgot:
Console.WriteLine (spacesBeGone('*', "")); Console.WriteLine (spacesBeGone('#', " ")); Console.WriteLine (spacesBeGone('^', " Mr John Smith "));i.e., empty string, all spaces, leading and/or trailing spaces ... if you test with the previous version, you will see the BUG.
revised code:
public static String spacesBeGone(Char placeHolder, String stringToCompact) { List<Char> compactedStringProxy = new List<Char>(); 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(); }new test
void Main() { Console.WriteLine (spacesBeGone('*', "")); Console.WriteLine (spacesBeGone('#', " ")); Console.WriteLine (spacesBeGone('^', " Mr John Smith ")); Console.WriteLine (spacesBeGone('%', "Mr John Smith")); Console.WriteLine (spacesBeGone('%', "Mr John Smith")); Console.WriteLine (spacesBeGone('¿', @"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.")); }new test output
g.
gerrylowry
All-Star
20513 Points
5712 Posts
Re: how can I replace space with other signs
May 03, 2012 05:56 AM|LINK
@ sdnd2000
note the test output from the previous example does not show the empty string that results from
Console.WriteLine (spacesBeGone('*', ""));this is not a BUG in my code ... imho, it's a bug in the forums.asp.net editor ....
if put this in the Message editor's "insert code" window:
--------------------------------------------------------------
#
^Mr^John^Smith^
Mr%John%Smith
Mr%John%Smith
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.
--------------------------------------------------------------
and i get this back
g.
Paul Linton
Star
13403 Points
2531 Posts
Re: how can I replace space with other signs
May 03, 2012 08:50 AM|LINK
var result = inStr.Replace(' ', '%');
gerrylowry
All-Star
20513 Points
5712 Posts
Re: how can I replace space with other signs
May 03, 2012 09:00 AM|LINK
@ Paul Linton
Hi Paul ... sorry, your suggestion does not work, because, ... 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.
g.
mbanavige
All-Star
134961 Points
15421 Posts
ASPInsiders
Moderator
MVP
Re: how can I replace space with other signs
May 03, 2012 10:02 AM|LINK
You can use regex
string testdata = "Mr John Smith"; testdata = System.Text.RegularExpressions.Regex.Replace(testdata, @"\s+", "%");http://msdn.microsoft.com/en-us/library/xwewhkd1.aspx
Paul Linton
Star
13403 Points
2531 Posts
Re: how can I replace space with other signs
May 03, 2012 10:54 AM|LINK
It does not say that. It says 'space to "%"' it does not say 'spaces to a single "%"'
Mastan Oli
Contributor
5088 Points
998 Posts
Re: how can I replace space with other signs
May 03, 2012 01:39 PM|LINK
here I used LINQ, very easiest and might be space or spaces :)
string obj1 = "Mr John Smith"; string obj2 = "Mr John Smith"; string obj3 = "MrJohn Smith"; var result1 = string.Join("%", obj1.Split(' ').Where(T => T != "").ToArray()); var result2 = string.Join("%", obj2.Split(' ').Where(T => T != "").ToArray()); var result3 = string.Join("%", obj3.Split(' ').Where(T => T != "").ToArray());playingOOPS | மெய்ப்பொருள் காண்பதறிவு
Mark as Answer If you find helpful