「C#」StringBuilderのAppendで文字列を連結する

2022年3月15日

書式
StringBuilder sb = new StringBuilder();
sb.Append(文字列);
System.Text.StringBuilderのAppendメソッドを使って文字列を単純に連結します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using System;
using System.Text;
namespace TestVal
{
class Program
{
static void Main(string[] args)
{
//StringBuilderを生成
StringBuilder sb = new StringBuilder();
//文字列を追加する
sb.Append("Windows11");
sb.Append(" windows10 ");
sb.Append(" apple ");
//string型に変換
string result = sb.ToString();
Console.WriteLine("StringBuilderで文字列を単純に連結​する結果:\n "+result);
}
}
}
using System; using System.Text; namespace TestVal { class Program { static void Main(string[] args) { //StringBuilderを生成 StringBuilder sb = new StringBuilder(); //文字列を追加する sb.Append("Windows11"); sb.Append(" windows10 "); sb.Append(" apple "); //string型に変換 string result = sb.ToString(); Console.WriteLine("StringBuilderで文字列を単純に連結​する結果:\n "+result); } } }
using System;
using System.Text;
namespace TestVal
{
  class Program
  {
    static void Main(string[] args)
    {
     
        //StringBuilderを生成
      StringBuilder sb = new StringBuilder();

       //文字列を追加する
        sb.Append("Windows11");
        sb.Append(" windows10 ");
         sb.Append(" apple ");

        //string型に変換
       string result = sb.ToString();
     
      Console.WriteLine("StringBuilderで文字列を単純に連結​する結果:\n "+result);
     
    }
  }
}

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
StringBuilderで文字列を単純に連結​する結果:
Windows11 windows10 apple
StringBuilderで文字列を単純に連結​する結果: Windows11 windows10 apple
StringBuilderで文字列を単純に連結​する結果:
 Windows11 windows10  apple

 

C#

Posted by arkgame