logo

Kako razdeliti niz v Golangu?

V jeziku Go, strune se razlikujejo od drugih jezikov, kot so Java , C++ , Python , itd. Je zaporedje znakov spremenljive širine, kjer je vsak znak predstavljen z enim ali več bajti z uporabo kodiranja UTF-8. V nizih Go lahko razdelite niz na rezino s pomočjo naslednjih funkcij. Te funkcije so definirane pod paketom nizov, zato morate uvoziti paket nizov v svoj program za dostop do teh funkcij:
1. Razdeli: Ta funkcija razdeli niz na vse podnize, ločene z danim ločilom, in vrne rezino, ki vsebuje te podnize.
Sintaksa:

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

tukaj, str je niz in sep je ločilo. če str ne vsebuje danega sep in sep ni prazen, potem vrne rezino dolžine 1, ki vsebuje samo str . Ali če je sep je prazen, potem se bo razdelil po vsakem zaporedju UTF-8. Ali če oboje str in sep so prazne, potem bo vrnil prazno rezino.
primer:

pojdi






// 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)> }>

>

razlika med podjetjem in družbo

>

Izhod:

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. RazdeliPo: Ta funkcija razdeli niz na vse podnize po vsakem primerku danega ločila in vrne rezino, ki vsebuje te podnize.
Sintaksa:

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

tukaj, str je niz in sep je ločilo. če str ne vsebuje danega sep in sep ni prazen, potem vrne rezino dolžine 1, ki vsebuje samo str . Ali če je sep je prazen, potem se bo razdelil po vsakem zaporedju UTF-8. Ali če oboje str in sep so prazne, potem bo vrnil prazno rezino.
primer:

pojdi




// 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)> }>

>

>

Izhod:

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. RazdeliPoN: Ta funkcija razdeli niz na vse podnize po vsakem primerku danega ločila in vrne rezino, ki vsebuje te podnize.
Sintaksa:

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

tukaj, str je niz, sep je ločilo, m pa se uporablja za iskanje števila podnizov, ki jih je treba vrniti. Tukaj, če m>0 , potem se kvečjemu vrne m podniz in zadnji podniz niza se ne bosta razdelila. če m == 0 , potem bo vrnil nič. če m<0 , potem bo vrnil vse podnize.
primer:

pojdi




enako metoda v Javi

// 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)> }>

>

>

Izhod:

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: []>