「C#」Replaceで文字列中の指定文字を置換する

書式
文字列.Replace(置換前文字, 置換後文字);
.Replaceを使用して文字列中の指定した文字を置換します。
Replaceは対象文字が存在しない場合でもエラーにはなりません。
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using System;
namespace Study
{
class Program
{
static void Main(string[] args)
{
//文字列
string str = "ADEADE";
//"A"を"S"に置換
string resA = str.Replace("A", "S");
Console.WriteLine(resA);
//"A"を""に置換
string resB = str.Replace("A", "");
Console.WriteLine(resB);
//"A"を"COME"に置換
string resC = str.Replace("A", "COME");
Console.WriteLine(resC);
}
}
}
using System; namespace Study { class Program { static void Main(string[] args) { //文字列 string str = "ADEADE"; //"A"を"S"に置換 string resA = str.Replace("A", "S"); Console.WriteLine(resA); //"A"を""に置換 string resB = str.Replace("A", ""); Console.WriteLine(resB); //"A"を"COME"に置換 string resC = str.Replace("A", "COME"); Console.WriteLine(resC); } } }
using System;

namespace Study
{
  class Program
  {
    static void Main(string[] args)
    {
        //文字列
        string str = "ADEADE";


        //"A"を"S"に置換
        string resA = str.Replace("A", "S");
         Console.WriteLine(resA);

         //"A"を""に置換
         string resB = str.Replace("A", "");
         Console.WriteLine(resB);

         //"A"を"COME"に置換
        string resC = str.Replace("A", "COME");
        Console.WriteLine(resC);
    }
  }
}

実行結果
SDESDE
DEDE
COMEDECOMEDE

C#

Posted by arkgame