Vb.net-strings
VB.Net-文字列
VB.Netでは、文字列を文字の配列として使用できますが、より一般的な方法は、Stringキーワードを使用して文字列変数を宣言することです。 文字列キーワードは、 System.String クラスのエイリアスです。
文字列オブジェクトの作成
次の方法のいずれかを使用して文字列オブジェクトを作成できます-
- 文字列リテラルを文字列変数に割り当てることにより
- Stringクラスコンストラクターを使用して
- 文字列連結演算子(+)を使用する
- プロパティを取得するか、文字列を返すメソッドを呼び出すことにより *書式設定メソッドを呼び出して、値またはオブジェクトを文字列表現に変換する
次の例はこれを示しています-
Module strings
Sub Main()
Dim fname, lname, fullname, greetings As String
fname = "Rowan"
lname = "Atkinson"
fullname = fname + " " + lname
Console.WriteLine("Full Name: {0}", fullname)
'by using string constructor
Dim letters As Char() = {"H", "e", "l", "l", "o"}
greetings = New String(letters)
Console.WriteLine("Greetings: {0}", greetings)
'methods returning String
Dim sarray() As String = {"Hello", "From", "Tutorials", "Point"}
Dim message As String = String.Join(" ", sarray)
Console.WriteLine("Message: {0}", message)
'formatting method to convert a value
Dim waiting As DateTime = New DateTime(2012, 12, 12, 17, 58, 1)
Dim chat As String = String.Format("Message sent at {0:t} on {0:D}", waiting)
Console.WriteLine("Message: {0}", chat)
Console.ReadLine()
End Sub
End Module
上記のコードをコンパイルして実行すると、次の結果が生成されます-
Full Name: Rowan Atkinson
Greetings: Hello
Message: Hello From Tutorials Point
Message: Message sent at 5:58 PM on Wednesday, December 12, 2012
文字列クラスのプロパティ
文字列クラスには、次の2つのプロパティがあります-
Sr.No | Property Name & Description |
---|---|
1 |
現在の_String_オブジェクトの指定された位置にある_Char_オブジェクトを取得します。 |
2 |
Length 現在のStringオブジェクトの文字数を取得します。 |
Stringクラスのメソッド
Stringクラスには、文字列オブジェクトの操作に役立つ多数のメソッドがあります。 次の表は、最も一般的に使用される方法のいくつかを提供します-
Sr.No | Method Name & Description |
---|---|
1 |
Public Shared Function Compare ( strA As String, strB As String ) As Integer 指定した2つの文字列オブジェクトを比較し、並べ替え順序での相対的な位置を示す整数を返します。 |
2 |
Public Shared Function Compare ( strA As String, strB As String, ignoreCase As Boolean ) As Integer 指定した2つの文字列オブジェクトを比較し、並べ替え順序での相対的な位置を示す整数を返します。 ただし、ブール値パラメーターがtrueの場合、大文字と小文字は無視されます。 |
3 |
Public Shared Function Concat ( str0 As String, str1 As String ) As String 2つの文字列オブジェクトを連結します。 |
4 |
Public Shared Function Concat ( str0 As String, str1 As String, str2 As String ) As String 3つの文字列オブジェクトを連結します。 |
5 |
Public Shared Function Concat (str0 As String, str1 As String, str2 As String, str3 As String ) As String 4つの文字列オブジェクトを連結します。 |
6 |
Public Function Contains ( value As String ) As Boolean 指定した文字列オブジェクトがこの文字列内に出現するかどうかを示す値を返します。 |
7 |
Public Shared Function Copy ( str As String ) As String 指定された文字列と同じ値を持つ新しいStringオブジェクトを作成します。 |
8 |
pPublic Sub CopyTo ( sourceIndex As Integer, destination As Char(), destinationIndex As Integer, count As Integer ) 文字列オブジェクトの指定された位置からUnicode文字の配列内の指定された位置に指定された数の文字をコピーします。 |
9 |
Public Function EndsWith ( value As String ) As Boolean 文字列オブジェクトの末尾が指定された文字列と一致するかどうかを判断します。 |
10 |
Public Function Equals ( value As String ) As Boolean 現在の文字列オブジェクトと指定された文字列オブジェクトが同じ値を持つかどうかを決定します。 |
11 |
Public Shared Function Equals ( a As String, b As String ) As Boolean 指定した2つの文字列オブジェクトの値が同じかどうかを判断します。 |
12 |
Public Shared Function Format ( format As String, arg0 As Object ) As String 指定された文字列内の1つ以上の書式項目を、指定されたオブジェクトの文字列表現に置き換えます。 |
13 |
Public Function IndexOf ( value As Char ) As Integer 現在の文字列で指定されたUnicode文字が最初に出現するゼロから始まるインデックスを返します。 |
14 |
Public Function IndexOf ( value As String ) As Integer このインスタンスで指定された文字列が最初に出現するゼロから始まるインデックスを返します。 |
15 |
Public Function IndexOf ( value As Char, startIndex As Integer ) As Integer 指定された文字位置で検索を開始し、この文字列で指定されたUnicode文字が最初に出現するゼロから始まるインデックスを返します。 |
16 |
Public Function IndexOf ( value As String, startIndex As Integer ) As Integer 指定された文字位置で検索を開始し、このインスタンスで指定された文字列が最初に出現するゼロから始まるインデックスを返します。 |
17 |
Public Function IndexOfAny ( anyOf As Char() ) As Integer Unicode文字の指定された配列内の任意の文字のこのインスタンスでの最初の出現のゼロから始まるインデックスを返します。 |
18 |
Public Function IndexOfAny ( anyOf As Char(), startIndex As Integer ) As Integer 指定されたUnicode文字の配列内の任意の文字のこのインスタンスでの最初の出現のゼロから始まるインデックスを返し、指定された文字位置で検索を開始します。 |
19 |
Public Function Insert ( startIndex As Integer, value As String ) As String 現在の文字列オブジェクトの指定されたインデックス位置に指定された文字列が挿入された新しい文字列を返します。 |
20 |
Public Shared Function IsNullOrEmpty ( value As String ) As Boolean 指定された文字列がnullか空の文字列かを示します。 |
21 |
Public Shared Function Join ( separator As String, ParamArray value As String() ) As String 各要素間に指定されたセパレータを使用して、文字列配列のすべての要素を連結します。 |
22 |
Public Shared Function Join ( separator As String, value As String(), startIndex As Integer, count As Integer ) As String 各要素間に指定された区切り文字を使用して、文字列配列の指定された要素を連結します。 |
23 |
Public Function LastIndexOf ( value As Char ) As Integer 現在の文字列オブジェクト内で指定されたUnicode文字が最後に出現するゼロから始まるインデックス位置を返します。 |
24 |
Public Function LastIndexOf ( value As String ) As Integer 現在の文字列オブジェクト内で指定された文字列が最後に出現するゼロから始まるインデックス位置を返します。 |
25 |
Public Function Remove ( startIndex As Integer ) As String 指定された位置から始まり、最後の位置まで続く現在のインスタンスのすべての文字を削除し、文字列を返します。 |
26 |
Public Function Remove ( startIndex As Integer, count As Integer ) As String 指定された位置から始まる現在の文字列の指定された文字数を削除し、文字列を返します。 |
27 |
Public Function Replace ( oldChar As Char, newChar As Char ) As String 現在の文字列オブジェクト内の指定されたUnicode文字のすべての出現を指定されたUnicode文字で置き換え、新しい文字列を返します。 |
28 |
Public Function Replace ( oldValue As String, newValue As String ) As String 現在の文字列オブジェクト内の指定された文字列のすべての出現を指定された文字列で置き換え、新しい文字列を返します。 |
29 |
Public Function Split ( ParamArray separator As Char() ) As String() 指定されたUnicode文字配列の要素で区切られた、現在の文字列オブジェクトの部分文字列を含む文字配列を返します。 |
30 |
Public Function Split ( separator As Char(), count As Integer ) As String() 指定されたUnicode文字配列の要素で区切られた、現在の文字列オブジェクトの部分文字列を含む文字配列を返します。 intパラメーターは、返される部分文字列の最大数を指定します。 |
31 |
Public Function StartsWith ( value As String ) As Boolean この文字列インスタンスの先頭が指定した文字列と一致するかどうかを判断します。 |
32 |
Public Function ToCharArray As Char() 現在の文字列オブジェクト内のすべての文字を含むUnicode文字配列を返します。 |
33 |
Public Function ToCharArray ( startIndex As Integer, length As Integer ) As Char() 指定されたインデックスから指定された長さまでの、現在の文字列オブジェクト内のすべての文字を含むUnicode文字配列を返します。 |
34 |
Public Function ToLower As String この文字列のコピーを小文字に変換して返します。 |
35 |
Public Function ToUpper As String この文字列のコピーを大文字に変換して返します。 |
36 |
Public Function Trim As String 現在のStringオブジェクトから先頭および末尾の空白文字をすべて削除します。 |
上記のメソッドのリストは完全なものではありません。メソッドの完全なリストとStringクラスコンストラクターについては、MSDNライブラリをご覧ください。
例
次の例は、上記の方法のいくつかを示しています-
文字列の比較
Module strings
Sub Main()
Dim str1, str2 As String
str1 = "This is test"
str2 = "This is text"
If (String.Compare(str1, str2) = 0) Then
Console.WriteLine(str1 + " and " + str2 + " are equal.")
Else
Console.WriteLine(str1 + " and " + str2 + " are not equal.")
End If
Console.ReadLine()
End Sub
End Module
上記のコードをコンパイルして実行すると、次の結果が生成されます-
This is test and This is text are not equal.
文字列には文字列が含まれています
Module strings
Sub Main()
Dim str1 As String
str1 = "This is test"
If (str1.Contains("test")) Then
Console.WriteLine("The sequence 'test' was found.")
End If
Console.ReadLine()
End Sub
End Module
上記のコードをコンパイルして実行すると、次の結果が生成されます-
The sequence 'test' was found.
サブストリングの取得:
Module strings
Sub Main()
Dim str As String
str = "Last night I dreamt of San Pedro"
Console.WriteLine(str)
Dim substr As String = str.Substring(23)
Console.WriteLine(substr)
Console.ReadLine()
End Sub
End Module
上記のコードをコンパイルして実行すると、次の結果が生成されます-
Last night I dreamt of San Pedro
San Pedro.
文字列の結合
Module strings
Sub Main()
Dim strarray As String() = {
"Down the way where the nights are gay",
"And the sun shines daily on the mountain top",
"I took a trip on a sailing ship",
"And when I reached Jamaica",
"I made a stop"
}
Dim str As String = String.Join(vbCrLf, strarray)
Console.WriteLine(str)
Console.ReadLine()
End Sub
End Module
上記のコードをコンパイルして実行すると、次の結果が生成されます-
Down the way where the nights are gay
And the sun shines daily on the mountain top
I took a trip on a sailing ship
And when I reached Jamaica
I made a stop