logo

C# |文字列の配列

アン 配列 同じ型の変数のコレクションです。一方、文字列は Unicode 文字のシーケンスまたは文字の配列です。したがって、文字列の配列は文字の配列の配列です。ここでは、文字列配列と文字列の配列は両方とも同じ用語です。

例えば 、クラスの生徒の名前を保存したい場合は、文字列の配列を使用できます。文字列の配列は 1 次元または多次元にすることができます。



文字列配列を宣言します。 文字列の配列を宣言するには、次の 2 つの方法があります。

    サイズのない宣言:

    構文:

    文字列[] 変数名;



    または

    文字列[] 変数名;

    サイズ付きの宣言:

    構文:



    String[] 変数名 = 新しい String[provide_size_here];

    または

    string[] 変数名 = 新しい文字列[provide_size_here];

例:

// 文字列キーワードを使用した宣言
文字列[] s1;

// Stringクラスオブジェクトを使用した宣言
// サイズを 4 に指定します
文字列[] s2 = 新しい文字列[4];

文字列配列の初期化: 配列は宣言後に初期化できます。 new キーワードを使用して宣言と初期化を同時に行う必要はありません。ただし、宣言後に配列を初期化する場合は、 new キーワードを使用して初期化する必要があります。値を代入するだけでは初期化できません。

例:

// 配列の宣言
文字列[] str1、str2;

// 配列の初期化
str1 = 新しい文字列[5]{ 要素 1、要素 2、要素 3、要素 4、要素 5 };

str2 = 新しい文字列[]{ 要素 1、要素 2、要素 3、要素 4、要素 5 };

注記: サイズを指定しない初期化は C# では無効です。コンパイル時にエラーが発生します。

例: 配列を初期化するための間違った宣言

// コンパイル時エラー: 配列のサイズを指定する必要があります
文字列[] str = 新しい文字列[];

// エラー : 配列の初期化が間違っています
文字列[] str1;
str1 = {要素 1、要素 2、要素 3、要素 4 };

文字列要素の配列へのアクセス: 初期化時に値を割り当てることができます。ただし、宣言と初期化の後に、インデックスを使用して配列の値をランダムに割り当てることもできます。要素のインデックスを配列名とともに角括弧内に配置することで、インデックスを作成することで配列値にアクセスできます。

文字列を整数に変換

例:

 // declares & initializes string array String[] s1 = new String[2]; // assign the value 'Geeks' in array on its index 0 s1[0] = 10; // assign the value 'GFG' in array on its index 1 s1[1] = 30; // assign the value 'Noida' in array on its index 2 s1[2] = 20; // Accessing array elements using index s1[0]; // returns Geeks s1[2]; // returns Noida>

文字列配列の宣言と初期化を 1 行で行います。 文字列配列は 1 行で宣言および初期化することもできます。コード行が減るため、この方法の方が推奨されます。

例:

 String[] weekDays = new string[3] {'Sun', 'Mon', 'Tue', 'Wed'};>

コード#1: 文字列配列の宣言、初期化、およびその要素へのアクセス




// C# program to illustrate the String array> // declaration, initialization and accessing> // its elements> using> System;> > class> Geeks {> > >// Main Method> >public> static> void> Main()> >{> >// Step 1: Array Declaration> >string>[] stringarr;> > >// Step 2:Array Initialization> >stringarr =>new> string>[3] {>'Element 1'>,>'Element 2'>,>'Element 3'>};> > >// Step 3:Accessing Array Elements> >Console.WriteLine(stringarr[0]);> >Console.WriteLine(stringarr[1]);> >Console.WriteLine(stringarr[2]);> >}> }>

>

>

出力:

 Element 1 Element 2 Element 3>

コード#2: 配列の宣言と初期化を 1 行で行う




// C# code to illustrate Array declaration> // and initialization in single line> using> System;> > class> Geeks {> > >// Main Method> >public> static> void> Main()> >{> >// array initialization and declaration> >String[] stringarr =>new> String[] {>'Geeks'>,>'GFG'>,>'Noida'>};> > >// accessing array elements> >Console.WriteLine(stringarr[0]);> >Console.WriteLine(stringarr[1]);> >Console.WriteLine(stringarr[2]);> >}> }>

>

>

出力:

 Geeks GFG Noida>

注記:

  • の中に public static void main(String[] args)> 文字列[] 引数 も文字列の配列です。

    例: 見せるために 文字列[] 引数 文字列の配列です。




    // C# program to get the type of 'args'> using> System;> > class> GFG {> > >// Main Method> >static> public> void> Main (String[] args) {> > >// using GetType() method to> >// get type at runtime> >Console.WriteLine(args.GetType());> >}> }>

    >

    >

    出力:

     System.String[]>
  • C# の文字列配列は基本的に オブジェクトの配列
  • を使用して文字列の配列を作成しているかどうかは関係ありません キーワードまたは 文字列クラスのオブジェクト 。どちらも同じです。

    例:




    // C# program to get the type of arrays of> // strings which are declared using 'string'> // keyword and 'String class object'> using> System;> > class> GFG {> > >// Main Method> >static> public> void> Main (String[] args) {> > >// declaring array of string> >// using string keyword> >string>[] s1 = {>'GFG'>,>'Noida'>};> > >// declaring array of string> >// using String class object> >String[] s2 =>new> String[2]{>'Geeks'>,>'C#'>};> > >// using GetType() method to> >// get type at runtime> >Console.WriteLine(s1.GetType());> >Console.WriteLine(s2.GetType());> >}> }>

    >

    >

    出力:

     System.String[] System.String[]>