0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C#:特定の文字を特定の文字数分増やして置換する

0
Posted at

特定の文字を特定の文字数分増やして置換する

(例)
「¶あいうえお」という文字列があって、特定の文字数分「¶」を増やしたい
※特定の文字例=「増えろ」

(結果)
「¶¶¶あいうえお」


Program.cs
using System;
using System.Linq;

public class Program{
    public static void Main(){

        string str = "¶あいうえお";
        string ward = "増えろ";

        // ¶¶¶に変換して置換
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        for (int i = 0; i < ward.Length; i++) sb.Append("¶");
        str = str.Replace("¶", sb.ToString());

        // ¶¶¶あいうえお
        System.Console.WriteLine(str);
    }
}

2回以上の文字列結合を行う場合はstring結合よりもStringBuilderを使ったほうが速いしメモリ使用量も抑えることができる

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?