Go 언어에서는 문자열 같은 다른 언어와는 다릅니다 자바 , C++ , 파이썬 등. 이는 각각의 모든 문자가 UTF-8 인코딩을 사용하여 하나 이상의 바이트로 표현되는 가변 너비 문자의 시퀀스입니다. 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)> }> |
>
자바와 같음
>
산출:
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. 분할후N: 이 함수는 주어진 구분 기호의 각 인스턴스 뒤에 문자열을 모든 하위 문자열로 분할하고 이러한 하위 문자열을 포함하는 조각을 반환합니다.
통사론:
log4j
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)> }> |
>
>
산출:
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: []>