「C#」文字列の範囲を指定して置換するサンプル

2021年10月6日

書式
StringBuilder 変数名 = new StringBuilder(文字列);
変数名.Replace(置換対象文字, 置換後文字, 開始位置, 終了位置);

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
StringBuilder str = new StringBuilder("study skill 123 study 456 skill");
//開始位置0 終了位置10
String result = str.Replace("study", "STUDY", 0, 10).ToString();
Console.WriteLine(result);
Console.ReadKey();
}
}
}
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { StringBuilder str = new StringBuilder("study skill 123 study 456 skill"); //開始位置0 終了位置10 String result = str.Replace("study", "STUDY", 0, 10).ToString(); Console.WriteLine(result); Console.ReadKey(); } } }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder str = new StringBuilder("study skill 123 study 456 skill");
            //開始位置0 終了位置10
            String result = str.Replace("study", "STUDY", 0, 10).ToString();
            Console.WriteLine(result);

            Console.ReadKey(); 
        }
    }
}

実行結果
STUDY skill 123 study 456 skill

C#

Posted by arkgame