Go string Processing

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

function :Join(a []string, sep string) string
Description : Separates the elements in a string slice from the character sep and merges them into a single string and returns
Example :

func main() {    str := []string{"Hello", "World", "Good"}    fmt.Println(strings.Join(str, " "))}

Program Output Hello World good

function :LastIndex(s, sep string) int
Description : Determines the position of the last occurrence of the character Sep in the string s, if the index of the SEP position is successfully returned, if the character Sep is not in the string s then return-1
Example :

func main() {    str := "Hello World"    fmt.Println(strings.LastIndex(str, "l"))}

Program Output 9

function : Repeat (S string, Count int) string
Description : Merges the count string s into a single string and returns
Example :

func main() {    str := "Hello "    fmt.Println(strings.Repeat(str, 5))}

Program output Hello hello hello hello hello

function : Replace (S, old, new string, n int) string
description : Replace the first n of the character old in the string s with the new character and return the replaced string, or N-1 if you want to replace all
Example :

func main() {    str := "hi hi hi are you ok"    fmt.Println(strings.Replace(str, "hi", "ok", 3))}

Program output OK ok ok

function : Split (S, Sep string) []string
Description : Splits a string in string s into a string slice of several elements with the character Sep as a delimiter, and returns a string slice
Example :

func main() {    str := "one,two,three"    for _, v := range strings.Split(str, ",") {        fmt.Println(v)    }}

Program output One, one, three

function : Splitafter (S, Sep string) []string
Description : Splits a string in string s into several string slices with character Sep as a delimiter and preserves the delimiter in the original string and returns a string slice
Example :

func main() {    str := "one,two,three"    for _, v := range strings.SplitAfter(str, ",") {        fmt.Println(v)    }}

Program output One, one, three

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.