This is my code

using (StringWriter strWriter = new StringWriter())
            {
                strWriter.WriteLine("Don't");
                // Get a copy of the contents (stored in a string) and pump to console
                Console.WriteLine("Content of StringWriter:\n{0}", strWriter);
                StringBuilder sb = strWriter.GetStringBuilder();
                Console.WriteLine("{0}", sb.Length);
                sb.Insert(0, "Hey!! ");
                Console.WriteLine("-> {0}", sb.ToString());
                sb.Remove(0, "Hey!! ".Length);
                Console.WriteLine("-> {0}", sb.ToString());
            }

I wanna insert "Hey!! " after "Don't" by changing...

sb.Insert(sb.Length - 1, "Hey!! ");
          Console.WriteLine("-> {0}", sb.ToString());
          sb.Remove(sb.Length - 1, "Hey!! ".Length);

but it still doesn't work true.

Member Avatar for stbuchok

What happens if you do sb.Length - 1 and Length is 0?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.