logo

Golang で文字列を分割するにはどうすればよいですか?

Go言語では、 文字列 のような他の言語とは異なります ジャワ C++ パイソン 、など。これは可変幅文字のシーケンスであり、すべての文字が UTF-8 エンコーディングを使用して 1 つ以上のバイトで表されます。 Go 文字列では、次の関数を使用して文字列をスライスに分割できます。これらの関数は strings パッケージで定義されているため、これらの関数にアクセスするにはプログラムに strings パッケージをインポートする必要があります。
1.分割: この関数は、文字列を指定された区切り文字で区切られたすべての部分文字列に分割し、これらの部分文字列を含むスライスを返します。
構文:

func Split(str, sep string) []string>

ここ、 str は文字列、sep は区切り文字です。もし str 指定されたものが含まれていません 9月 そして 9月 が空ではない場合、次のものだけを含む長さ 1 のスライスが返されます。 str 。あるいは、 9月 が空の場合、UTF-8 シーケンスごとに分割されます。あるいは両方の場合 str そして 9月 が空の場合、空のスライスが返されます。
例:



行く








// Go program to illustrate how to split a string> package> main> import> (> >'fmt'> >'strings'> )> // Main function> func> main() {> >// Creating and initializing the strings> >str1 :=>'Welcome, to the, online portal, of techcodeview.com'> >str2 :=>'My dog name is Dollar'> >str3 :=>'I like to play Ludo'> >// Displaying strings> >fmt.Println(>'String 1: '>, str1)> >fmt.Println(>'String 2: '>, str2)> >fmt.Println(>'String 3: '>, str3)> >// Splitting the given strings> >// Using Split() function> >res1 := strings.Split(str1,>','>)> >res2 := strings.Split(str2,>''>)> >res3 := strings.Split(str3,>'!'>)> >res4 := strings.Split(>''>,>'techcodeview.com, geeks'>)> >// Displaying the result> >fmt.Println(>' Result 1: '>, res1)> >fmt.Println(>'Result 2: '>, res2)> >fmt.Println(>'Result 3: '>, res3)> >fmt.Println(>'Result 4: '>, res4)> }>

>

CのASCIIテーブル

>

出力:

String 1: Welcome, to the, online portal, of techcodeview.com String 2: My dog name is Dollar String 3: I like to play Ludo  Result 1: [Welcome to the online portal of techcodeview.com] Result 2: [M y d o g n a m e i s D o l l a r] Result 3: [I like to play Ludo] Result 4: []>

2.分割後: この関数は、指定された区切り文字の各インスタンスの後で文字列をすべての部分文字列に分割し、これらの部分文字列を含むスライスを返します。
構文:

func SplitAfter(str, sep string) []string>

ここ、 str は文字列、sep は区切り文字です。もし str 指定されたものが含まれていません 9月 そして 9月 が空ではない場合、次のものだけを含む長さ 1 のスライスが返されます。 str 。あるいは、 9月 が空の場合、UTF-8 シーケンスごとに分割されます。あるいは両方の場合 str そして 9月 が空の場合、空のスライスが返されます。
例:

行く




// Go program to illustrate how to split a string> package> main> import> (> >'fmt'> >'strings'> )> // Main function> func> main() {> >// Creating and initializing the strings> >str1 :=>'Welcome, to the, online portal, of techcodeview.com'> >str2 :=>'My dog name is Dollar'> >str3 :=>'I like to play Ludo'> >// Displaying strings> >fmt.Println(>'String 1: '>, str1)> >fmt.Println(>'String 2: '>, str2)> >fmt.Println(>'String 3: '>, str3)> >// Splitting the given strings> >// Using SplitAfter() function> >res1 := strings.SplitAfter(str1,>','>)> >res2 := strings.SplitAfter(str2,>''>)> >res3 := strings.SplitAfter(str3,>'!'>)> >res4 := strings.SplitAfter(>''>,>'techcodeview.com, geeks'>)> >// Displaying the result> >fmt.Println(>' Result 1: '>, res1)> >fmt.Println(>'Result 2: '>, res2)> >fmt.Println(>'Result 3: '>, res3)> >fmt.Println(>'Result 4: '>, res4)> }>

>

>

出力:

String 1: Welcome, to the, online portal, of techcodeview.com String 2: My dog name is Dollar String 3: I like to play Ludo  Result 1: [Welcome, to the, online portal, of techcodeview.com] Result 2: [M y d o g n a m e i s D o l l a r] Result 3: [I like to play Ludo] Result 4: []>

3.SplitAfterN: この関数は、指定された区切り文字の各インスタンスの後で文字列をすべての部分文字列に分割し、これらの部分文字列を含むスライスを返します。
構文:

func SplitAfterN(str, sep string, m int) []string>

ここ、 str 文字列です、 9月 は区切り文字で、m は返される部分文字列の数を見つけるために使用されます。ここで、もし m>0 、その後、最大でも戻ります メートル 部分文字列と最後の文字列の部分文字列は分割されません。もし m == 0 、その場合、nil が返されます。もし m<0 の場合、すべての部分文字列が返されます。
例:

行く




// Go program to illustrate how to split a string> package> main> import> (> >'fmt'> >'strings'> )> // Main function> func> main() {> >// Creating and initializing the strings> >str1 :=>'Welcome, to the, online portal, of techcodeview.com'> >str2 :=>'My dog name is Dollar'> >str3 :=>'I like to play Ludo'> >// Displaying strings> >fmt.Println(>'String 1: '>, str1)> >fmt.Println(>'String 2: '>, str2)> >fmt.Println(>'String 3: '>, str3)> >// Splitting the given strings> >// Using SplitAfterN() function> >res1 := strings.SplitAfterN(str1,>','>,>2>)> >res2 := strings.SplitAfterN(str2,>''>,>4>)> >res3 := strings.SplitAfterN(str3,>'!'>,>1>)> >res4 := strings.SplitAfterN(>''>,>'techcodeview.com, geeks'>,>3>)> >// Displaying the result> >fmt.Println(>' Result 1: '>, res1)> >fmt.Println(>'Result 2: '>, res2)> >fmt.Println(>'Result 3: '>, res3)> >fmt.Println(>'Result 4: '>, res4)> }>

>

>

1次ロジック

出力:

String 1: Welcome, to the, online portal, of techcodeview.com String 2: My dog name is Dollar String 3: I like to play Ludo  Result 1: [Welcome, to the, online portal, of techcodeview.com] Result 2: [M y dog name is Dollar] Result 3: [I like to play Ludo] Result 4: []>