以下は新人エンジニア向けのJavaテキストをC#に書き換えたものです。


なぜ、文字列の理解が重要なのか、その理由

この記事では、弊社の新人エンジニア研修の参考にC#を解説します。

前回は配列の作成と使用について解説しました。
今回は文字と文字列の扱いについて解説します。文字列はコンピュータプログラム(すなわちそれを作った新人エンジニアであるあなた)とユーザーとのコミュニケーション手段ですからとても重要です。


1. 文字と文字列の違い

C#では、文字と文字列は別物です。以下のサンプルプログラムを見てください。

using System;

namespace Chap07
{
    public class Example01
    {
        public static void Main(string[] args)
        {
            char c1 = 'あ';
            string str1 = "あ";

            Console.WriteLine(c1);
            Console.WriteLine(str1);
        }
    }
}

<実行結果>

どちらも出力結果は同じですが、c1にはひらがな「あ」の文字コードが、str1には文字列「あ」への参照が入っています。文字は値型、文字列は参照型なのでした。

次に、以下のプログラムを実行するとどうなるでしょうか?

using System;

namespace Chap07
{
    public class Example02
    {
        public static void Main(string[] args)
        {
            char c1 = 'あ';
            int a = c1;
            Console.WriteLine(a);
        }
    }
}

<実行結果>
12354

この結果「12354」は、「あ」のUTF-16における文字コードを10進数で表現したものです。


実験1

文字列も内部的には文字の配列として扱われていることを理解しましょう。例えば、以下のExample03の出力結果を予想してください。

using System;

namespace Chap07
{
    public class Example03
    {
        public static void Main(string[] args)
        {
            char[] charstr = { 'H', 'e', 'l', 'l', 'o' };
            string str = new string(charstr);
            Console.WriteLine(str);
        }
    }
}

<実行結果>
Hello


2. Stringクラス

C#のstring型は、System.Stringクラスのエイリアスです。以下のExample04はStringクラスのコンストラクタを使って文字列を生成する例です。

using System;

namespace Chap07
{
    public class Example04
    {
        public static void Main(string[] args)
        {
            string str = new string(new char[] { 'H', 'e', 'l', 'l', 'o' });
            Console.WriteLine(str);
        }
    }
}

頻繁に使うので、以下のように簡単にインスタンス化する方法が一般的です。

using System;

namespace Chap07
{
    public class Example05
    {
        public static void Main(string[] args)
        {
            string str2 = "Hello";
            Console.WriteLine(str2);
        }
    }
}


3. Equalsメソッドによる文字列比較

以下のExample06では、文字列を比較するプログラムです。

using System;

namespace Chap07
{
    public class Example06
    {
        public static void Main(string[] args)
        {
            string str1 = new string(new char[] { 'H', 'e', 'l', 'l', 'o' });
            string str2 = new string(new char[] { 'H', 'e', 'l', 'l', 'o' });
            Console.WriteLine(str1 == str2);

            string str3 = "Hello";
            string str4 = "Hello";
            Console.WriteLine(str3 == str4);
        }
    }
}

<実行結果>
true
true


4. イミュータブル

以下のExample08を見てください。

using System;

namespace Chap07
{
    public class Example08
    {
        public static void Main(string[] args)
        {
            string str1 = "Hello";
            str1 = str1 + " World";
            Console.WriteLine(str1);
        }
    }
}

<実行結果>
Hello World

C#のstringもイミュータブル(不変性)です。一度作成したインスタンスの内容は変更されません。


5. Stringクラスの便利なメソッド

以下は文字列操作の例です。

using System;

namespace Chap07
{
    public class Example10
    {
        public static void Main(string[] args)
        {
            string str = "新人エンジニアのためのC#研修";

            Console.WriteLine(str.Length);

            Console.WriteLine(str.IndexOf("C#"));
            Console.WriteLine(str.IndexOf("Python"));

            Console.WriteLine(str.Contains("研修"));
            Console.WriteLine(str.Contains("Python"));

            string str2 = str.Replace("エンジニア", "SE");
            Console.WriteLine(str2);
        }
    }
}

<実行結果>
17
11
-1
true
false
新人SEのためのC#研修






<まとめ:隣の人に正しく説明できたらチェックを付けましょう>

□ 2つの文字列が同一の文字列であるか確かめるためには==演算子ではなく、Equals()メソッドを使わなければならない

string クラスには、char のようなプリミティブ型とは違い文字列を扱うための便利なメソッドが用意されている

static メソッドは、クラス名.メソッド名() という形で呼び出すことができ、インスタンスメソッドは、変数名.メソッド名() という形で呼び出すことができる

□ 特別な記号や出力方法を制御するためには \ 記号を使ってエスケープシーケンスを作る

□ 参照型に null を代入することで、その参照はインスタンスを参照しなくなり、どこからも参照されなくなったインスタンスはいずれガベージコレクションによってメモリから消去される

これらの基礎をしっかり押さえておくことで、C#プログラミングの理解が深まります。

まとめができたら、アウトプットとして演習問題にチャレンジしましょう。

最後までお読みいただきありがとうございます。