stringbuilder - Increase the size of a string in c# -
i trying write method replaces blank space in %20 in c#. using stringbuilder build new string. running out of range exception, because size of array runs out. there way increase size of string build using string
static void main(string[] args) { string add = "mr john smtih"; console.writeline(add); stringbuilder sb = new stringbuilder(add); int j = 0; (int = 0; < add.length; i++) { if (add[i].equals(' ')) { sb[j] = '%'; j++; sb[j] = '2'; j++; sb[j] = '0'; } else { sb[j] = add[i]; } j++; } console.writeline(add); console.writeline(sb); console.readline(); }
the string
class immutable, once initialize cannot change size of it.
you should use append()
method in stringbuilder
. code this:
static void main(string[] args) { string add = "mr john smtih"; console.writeline(add); stringbuilder sb = new stringbuilder(); (int = 0; < add.length; i++) { if (add[i].equals(' ')) { sb.append("%20"); } else { sb.append(add[i]); } } console.writeline(add); console.writeline(sb); console.readline(); }
Comments
Post a Comment