public static string kw;
public String parse(String keyword)
{
this.keyword = keyword;
char[] letters = keyword.ToCharArray();
string g;
long length = System.Convert.ToInt64(keyword.Length.ToString());
for (int i = 0; i <= length-1; i++)
{
kw = "/"+letters[i];
}
return kw;
}
So if the keyword is lets say, “Hello”. I want this to output /h/e/l/l/o but at the moment its only outputting the last character, in this case /o
Can someone help?
on
=vs+=andStringvsStringBuilderYour problem is in this line:
This is a straight assignment, and will overwrite the value of
kwfrom the previous iteration. Perhaps you want+=. However, at this point you need to know aboutStringBuilderand why doing+=withStringin a loop yields bad performance.Related questions
On regular expressions
If you’re up to learning regular expression, you can also do this with one line. You simply match each character
xand replace it with/x.References
Regex.ReplaceExample
Here’s a snippet that should be illustrative:
Some key ideas:
StringBuilderReferences
StringBuilderClassAttachments