String.Substring メソッドとは? わかりやすく解説

String.Substring メソッド (Int32, Int32)

インスタンスか部分文字列取得します。この部分文字列は、指定した文字位置から開始し指定した文字数文字列です。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Function Substring ( _
    startIndex As Integer, _
    length As Integer _
) As String
Dim instance As String
Dim startIndex As Integer
Dim length As Integer
Dim returnValue As String

returnValue = instance.Substring(startIndex, length)
public string Substring (
    int startIndex,
    int length
)
public:
String^ Substring (
    int startIndex, 
    int length
)
public String Substring (
    int startIndex, 
    int length
)
public function Substring (
    startIndex : int, 
    length : int
) : String

パラメータ

startIndex

部分文字列開始位置インデックス

length

部分文字列文字数

戻り値
このインスタンスstartIndex から長さ length抽出することによって得られる部分文字列等しString。または、startIndex がこのインスタンス長さ等しくlengthゼロ場合Empty

例外例外
例外種類条件

ArgumentOutOfRangeException

startIndexlength足した数が、このインスタンス内にない位置示してます。

または

startIndex または length が 0 未満です。

解説解説

startIndex が 0 から始まってます。

使用例使用例

Substring メソッド使用して文字列から部分文字列取り出す 3 つのコード例次に示しますそのうち 2 つは、比較理に部分文字列使用してます。3 つ目の例では、無効なパラメータ指定しているため例外スローさます。

Dim myString As String =
 "abc"
Dim test1 As Boolean = String.Compare(myString.Substring(2,
 1), "c") = 0 ' This is true.
myString.Substring(3, 1) ' This throws ArgumentOutOfRangeException.
Dim test2 As Boolean = String.Compare(myString.Substring(3,
 0), String.Empty) = 0 ' This is true.
String myString = "abc";
bool test1 = String.Compare(myString.Substring(2, 1), "c")
 == 0; // This is true.
myString.Substring(3, 1); // This throws ArgumentOutOfRangeException.
bool test2 = String.Compare(myString.Substring(3, 0), String.Empty)
 == 0; // This is true.
String^ myString = "abc";
bool test1 = String::Compare( myString->Substring( 2, 1 ),
 "c" ) == 0; // This is true.

myString->Substring( 3, 1 ); // This throws ArgumentOutOfRangeException.
bool test2 = String::Compare( myString->Substring( 3, 0 ),
 String::Empty ) == 0; // This is true.
String myString = "abc";
// This is true.
boolean test1 = String.Compare(myString.Substring(2, 1), "c") == 0;   
     
myString.Substring(3, 1); // This throws ArgumentOutOfRangeException.
// This is true.
boolean test2 = String.Compare(myString.Substring(3, 0), " ") == 0;        
var myString : String = "abc";
var test1 : boolean = String.Compare(myString.Substring(2, 1),
 "c") == 0; // This is true.
myString.Substring(3, 1);  // This throws ArgumentOutOfRangeException.
var test2 : boolean = String.Compare(myString.Substring(3, 0),
 String.Empty) == 0; // This is true.
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.Substring メソッド (Int32)

インスタンスか部分文字列取得します検索は、指定した文字位置から開始されます。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Function Substring ( _
    startIndex As Integer _
) As String
Dim instance As String
Dim startIndex As Integer
Dim returnValue As String

returnValue = instance.Substring(startIndex)
public string Substring (
    int startIndex
)
public:
String^ Substring (
    int startIndex
)
public String Substring (
    int startIndex
)
public function Substring (
    startIndex : int
) : String

パラメータ

startIndex

このインスタンス内の部分文字列開始文字位置

戻り値
このインスタンスstartIndex で始まる部分文字列等しString オブジェクト。または、startIndex がこのインスタンス長さ等し場合Empty

例外例外
例外種類条件

ArgumentOutOfRangeException

startIndex が、0 未満か、このインスタンス長さ超えてます。

解説解説
使用例使用例

文字列部分文字列取得するコード例次に示します

Imports System

Public Class SubStringTest
    
    Public Shared Sub Main()
        Dim info As String()
 = {"Name: Felica Walker", "Title:
 Mz.", "Age: 47", "Location: Paris", "Gender: F"}
        Dim found As Integer
 = 0
       
        Console.WriteLine("The initial values in the array are:")
        Dim s As String
        For Each s In  info
            Console.WriteLine(s)
        
        Next s
        Console.WriteLine("{0}We want to retrieve only the key
 information. That is:", Environment.NewLine)
        
        For Each s In  info
            found = s.IndexOf(":")
            Console.WriteLine(s.Substring((found + 1)).Trim())
        Next s
    End Sub 'Main
End Class 'SubStringTest
using System;

public class SubStringTest {
    public static void Main()
 {

        string [] info = {"Name: Felica Walker", "Title:
 Mz.", "Age: 47", "Location: Paris", "Gender: F"};
        int found = 0;

        Console.WriteLine("The initial values in the array
 are:");
        foreach (string s in
 info)
            Console.WriteLine(s);

        Console.WriteLine("{0}We want to retrieve only the key information.
 That is:", Environment.NewLine);        

        foreach (string s in
 info) {
            found = s.IndexOf(":");
            Console.WriteLine(s.Substring(found + 1).Trim());
        }
    }
}
using namespace System;
using namespace System::Collections;
int main()
{
   array<String^>^info = {"Name: Felica Walker","Title: Mz.","Age:
 47","Location: Paris","Gender: F"};
   int found = 0;
   Console::WriteLine( "The initial values in the array are:"
 );
   IEnumerator^ myEnum1 = info->GetEnumerator();
   while ( myEnum1->MoveNext() )
   {
      String^ s = safe_cast<String^>(myEnum1->Current);
      Console::WriteLine( s );
   }

   Console::WriteLine( "\nWe want to retrieve only the key information. That
 is:" );
   IEnumerator^ myEnum2 = info->GetEnumerator();
   while ( myEnum2->MoveNext() )
   {
      String^ s = safe_cast<String^>(myEnum2->Current);
      found = s->IndexOf( ":" );
      Console::WriteLine( s->Substring( found + 1 )->Trim() );
   }
}

import System.*;

public class SubStringTest
{
    public static void main(String[]
 args)
    {
        String info[] =  { "Name: Felica Walker", "Title: Mz."
,
            "Age: 47", "Location: Paris", "Gender: F"
 };
        int found = 0;

        Console.WriteLine("The initial values in the array
 are:");
        for (int iCtr = 0; iCtr < info.get_Length();
 iCtr++) {
            String s = info[iCtr];
            Console.WriteLine(s);
        }

        Console.WriteLine("{0}We want to retrieve only the key information."
 
            + " That is:", Environment.get_NewLine());
        for (int iCtr = 0; iCtr < info.get_Length();
 iCtr++) {
            String s = info[iCtr];
            found = s.IndexOf(":");
            Console.WriteLine(s.Substring(found + 1).Trim());
        }
    } //main
} //SubStringTest
import System;

public class SubStringTest {
    public static function
 Main() : void {

        var info : String [] = ["Name: Felica Walker",
 "Title: Mz.", "Age: 47", "Location: Paris", "Gender:
 F"];
        var found : int = 0;

        Console.WriteLine("The initial values in the array are:");
        for (var i : int
 in info)
            Console.WriteLine(info[i]);

        Console.WriteLine("{0}We want to retrieve only the key information.
 That is:", Environment.NewLine);        

        for (i in info) {
            found = info[i].IndexOf(":");
            Console.WriteLine(info[i].Substring(found + 1).Trim());
        }
    }
}
SubStringTest.Main();
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.Substring メソッド




英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「String.Substring メソッド」の関連用語

String.Substring メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



String.Substring メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS