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

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

または

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 メソッド




英和和英テキスト翻訳

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

辞書ショートカット

すべての辞書の索引

「String.Substring」の関連用語

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

   

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



String.Substringのページの著作権

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

©2026 GRAS Group, Inc.RSS