1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【C#】Stringクラスで文字列処理

Last updated at Posted at 2025-06-01

はじめに

C#のスキルチェックで新たに学んだことを紹介する記事です。
記事の内容に誤りがあるかもしれませんが、ご了承ください。
(誤りがありましたら修正いたします:bow_tone1:

String クラス

Stringクラスとは、文字列の分割や結合、比較、検索など文字列を扱うメソッドを使うことができます。詳しくは公式の解説をご確認ください。

プログラミング経験の浅い私にとっては量が多すぎるので、ここではC#のスキルチェックを通じて学んだメソッドをまとめていきます。

Splitメソッドで文字列を分割

Splitメソッドは、文字列を指定した文字列で区切り、文字列の配列を返します。
区切り文字が連続した場合、分割結果に空の文字列が含まれることになります。

半角スペース区切り

Sample1.cs
using System;
class Sample1
{
    static void Main()
    {
        // "1 2 3"と入力し、半角スペース区切りで3つの変数に格納する場合
        int x, y, z;
        string[] s = Console.ReadLine().Split(' ');
        x = int.Parse(s[0]);
        y = int.Parse(s[1]);
        z = int.Parse(s[2]);
        Console.WriteLine(x + "," + y + "," + z); // 出力結果:1,2,3
    }
}

カンマ区切り

Sample2.cs
using System;
class Sample2
{
    static void Main()
    {
        // "4,5,6"と入力し、カンマ区切りで3つの変数に格納する場合
        int x, y, z;
        string[] s = Console.ReadLine().Split(',');
        x = int.Parse(s[0]);
        y = int.Parse(s[1]);
        z = int.Parse(s[2]);
        Console.WriteLine(x + " " + y + " " + z); // 出力結果:4 5 6
    }
}

Joinメソッドで指定文字列を挟んで結合

Joinメソッドは、複数の文字列を指定した区切り文字で結合して返します。
区切り文字がnullの場合、空の文字列が使用され文字列のみ結合されます。

Sample3.cs
using System;
class Sample3
{
    static void Main()
    {
        // 文字列の配列
        string[] strings = new string[]{"12", "345", "67"};
        // "/"を挟んで連結
        Console.WriteLine(string.Join("/", strings); // 出力結果:12/345/67
        // 区切り文字をnullにした場合
        Console.WriteLine(string.Join(null, strings); // 出力結果:1234567
    }
}

Equalsメソッドで文字列同士が等しいか判定

Equalsメソッドは、2つの文字列が等しいかどうかを判断するメソッドです。

Sample4.cs
using System;
class Sample4
{
    static void Main()
    {
        string str1 = "ABC";
        string str2 = "ABC";
        if(str1.Equals(str2))
        {
            Console.WriteLine("str1とstr2は等しい");  
        }
        else
        {
            Console.WriteLine("str1とstr2は異なる");
        }
    }
}

Containsメソッドで文字列を含むか判定

Containsメソッドを使うと、文字列の中に指定した文字/文字列が存在するかどうかを確認することができます。なお、検索の際は大文字と小文字を区別します。

Sample5.cs
using System;
class Sample5
{
    static void Main()
    {
        string S = "ABCdef";
        Console.WriteLine(S.Contains('A')); // 出力結果:true
        Console.WriteLine(S.Contains("ABC")); // 出力結果:true
        Console.WriteLine(S.Contains("DEF")); // 出力結果:false
    }
}

Replaceメソッドで文字列を置換

Replaceメソッドを使用して、文字/文字列を置換します。
以下のサンプルコードは時々SNSで見かける(?)Leetと呼ばれるインターネットスラングです。

Sample6.cs
using System;
class Sample6
{
    static void Main()
    {
        string S = "AEGIOSZ";
        S = S.Replace("A", "4");
        S = S.Replace("E", "3");
        S = S.Replace("G", "6");
        S = S.Replace("I", "1");
        S = S.Replace("O", "0");
        S = S.Replace("S", "5");
        S = S.Replace("Z", "2");
            
        Console.WriteLine(S); // 出力結果:4361052
    }
}

Removeメソッドで文字列を削除

Removeメソッドは、開始位置と削除する文字数を指定することで文字列の一部分を削除することができます。

Sample7.cs
using System;
class Sample7
{
    static void Main()
    {
        string S = "ABC123";
        // 2文字目から3文字削除
        Console.WriteLine(S.Remove(1, 3)); // 出力結果:A23
    }
}

まとめ

ここでは、C#のスキルチェックを通じて文字列の処理を新しく学んだので、Stringクラスのメソッドについて紹介しました。
Stringクラスのメソッドは今回紹介したメソッド以外にもまだまだありますが、引き続き学習して使いこなしていけたらと思います。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?