❔ Stringbuilder remove
i have problem with remove in the following program:
public class Program11
{
static void Main(string[] args) { StringBuilder sb = new StringBuilder("Hello World!", 50); //"Hallo Welt! start String //50 maximale Länge des Strings sb.Append(" How are u?"); // string hinten anfügen sb.AppendLine(); sb.AppendLine("I am fine!"); //string hinten anfügen zeilenumbruch sb.Insert(0, ":) ");
Console.WriteLine(sb.ToString()); sb.Remove(9, 14); Console.WriteLine(sb.ToString()); } }
} I wanna remove World witch should go from 9-14: output before remove; 🙂 Hello World! How are u? I am fine! output after remove: 🙂 Hello u? I am fine!
static void Main(string[] args) { StringBuilder sb = new StringBuilder("Hello World!", 50); //"Hallo Welt! start String //50 maximale Länge des Strings sb.Append(" How are u?"); // string hinten anfügen sb.AppendLine(); sb.AppendLine("I am fine!"); //string hinten anfügen zeilenumbruch sb.Insert(0, ":) ");
Console.WriteLine(sb.ToString()); sb.Remove(9, 14); Console.WriteLine(sb.ToString()); } }
} I wanna remove World witch should go from 9-14: output before remove; 🙂 Hello World! How are u? I am fine! output after remove: 🙂 Hello u? I am fine!
8 Replies
":)"
when i compiling it
Check the indexes, keep in mind the line terminator characters added with AppendLine
how do i check
what is the terminator character
Either \n or \r\n Just add the result to a string variable and add that variable to a Watch
or just add "sb.ToString()" to the watch
Watch displays the string as is on a single line with special characters as their escape form (backslash)
what is the watch
//50 maximale Länge des Strings
note that the 50
constructor parameter is not the maximum capacity of the StringBuilder
but it's initial capacity
which line separator (and thus its length) is used is specified by Environment.NewLine
from the System
namespace
watch is a debugger feature, its a table where u can add variables/expressions, which then shows their values as well
basically like
I wanna remove World witch should go from 9-14:also check what the parameters for
StringBuilder.Remove()
are... the latter is not the end index 😉
btw ur commenting style is also a bit weird.
basically everyone else writes the comment above the code they want to give a comment for ;pThat or beside the line
foo(); // do foo
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.