Ako rozdeliť strunu v Golangu?
V jazyku Go struny sa líšia od iných jazykov, napr Java , C++ , Python , atď. Ide o postupnosť znakov s premenlivou šírkou, kde každý znak je reprezentovaný jedným alebo viacerými bajtmi pomocou kódovania UTF-8. V reťazcoch Go máte povolené rozdeliť reťazec na rez pomocou nasledujúcich funkcií. Tieto funkcie sú definované v balíku reťazcov, takže ak chcete získať prístup k týmto funkciám, musíte do programu importovať balík reťazcov:
1. Rozdelenie: Táto funkcia rozdelí reťazec na všetky podreťazce oddelené daným oddeľovačom a vráti výsek, ktorý obsahuje tieto podreťazce.
Syntax:
func Split(str, sep string) []string
Tu, str je reťazec a sep je oddeľovač. Ak str neobsahuje dané sept a sept je neprázdny, potom vráti výsek s dĺžkou 1, ktorý obsahuje iba str . Alebo ak sept je prázdna, potom sa rozdelí po každej sekvencii UTF-8. Alebo ak oboje str a sept sú prázdne, potom vráti prázdny plátok.
Príklad:
Choď
// 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)> }> |
Výkon:
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. RozdeleniePo: Táto funkcia rozdelí reťazec na všetky podreťazce po každom výskyte daného oddeľovača a vráti výsek, ktorý obsahuje tieto podreťazce.
Syntax:
func SplitAfter(str, sep string) []string
Tu, str je reťazec a sep je oddeľovač. Ak str neobsahuje dané sept a sept je neprázdny, potom vráti výsek s dĺžkou 1, ktorý obsahuje iba str . Alebo ak sept je prázdna, potom sa rozdelí po každej sekvencii UTF-8. Alebo ak oboje str a sept sú prázdne, potom vráti prázdny plátok.
Príklad:
Choď
// 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)> }> |
Výkon:
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: Táto funkcia rozdelí reťazec na všetky podreťazce po každom výskyte daného oddeľovača a vráti výsek, ktorý obsahuje tieto podreťazce.
Syntax:
func SplitAfterN(str, sep string, m int) []string
Tu, str je reťazec, sept je oddeľovač a m sa používa na nájdenie počtu podreťazcov, ktoré sa majú vrátiť. Tu, ak m>0 , potom sa vráti maximálne m podreťazce a posledný podreťazec reťazca sa nerozdelí. Ak m = = 0 , potom sa vráti nula. Ak m <0 , potom vráti všetky podreťazce.
Príklad:
Choď
// 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)> }> |
Výkon:
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: []