Answers for "stringbuilder csharp"

C#
11

c# stringbuilder

using System;
using System.Text;

public sealed class App 
{
    static void Main() 
    {
        // Create a StringBuilder that expects to hold 50 characters.
        // Initialize the StringBuilder with "ABC".
        StringBuilder sb = new StringBuilder("ABC", 50);

        // Append three characters (D, E, and F) to the end of the StringBuilder.
        sb.Append(new char[] { 'D', 'E', 'F' });

        // Append a format string to the end of the StringBuilder.
        sb.AppendFormat("GHI{0}{1}", 'J', 'k');

        // Display the number of characters in the StringBuilder and its string.
        Console.WriteLine("{0} chars: {1}", sb.Length, sb.ToString());

        // Insert a string at the beginning of the StringBuilder.
        sb.Insert(0, "Alphabet: ");

        // Replace all lowercase k's with uppercase K's.
        sb.Replace('k', 'K');

        // Display the number of characters in the StringBuilder and its string.
        Console.WriteLine("{0} chars: {1}", sb.Length, sb.ToString());
    }
}

// This code produces the following output.
//
// 11 chars: ABCDEFGHIJk
// 21 chars: Alphabet: ABCDEFGHIJK
Posted by: Guest on March-12-2020
1

c# stringbuilder

using System;
using System.Text;
					
public class Program
{
	public static void Main()
	{
		string[] animals = {"Cow","Goat","angry dog"};
		StringBuilder builder = new StringBuilder();
		//appending
		builder.Append("a fox jumped over a ");
		builder.Append("lazy dog");
		//append line can append a line ending
		builder.AppendLine();
		//appendjoin can itrate over a set of values
		builder.Append("he also jumped on ");
		builder.AppendJoin(",",animals);
		//can modify the content by 
		builder.Replace("fox ","cat ");
		//convert to string
		builder.AppendLine();
		builder.Append("the angy dog chased him for a long time but the cat excaped");
		Console.WriteLine(builder.ToString());
	}
}
Posted by: Guest on November-01-2021

C# Answers by Framework

Browse Popular Code Answers by Language