Get the substring character position and intercept substring of Chinese string in Golang

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

Yesterday I was going to do a simple text analysis with Golang, I needed to do some simple manipulation of the string, and when I looked at the strings and StrConv libraries, I didn't find the function to intercept the string, and strings. Index returns the byte position of the substring, such as this example: strings. Index ("Good morning, Mr. Zhang!") The return value of "good" is 6, not 2 (starting from 0).


So I wrote a function to deal with the position of the return string substring in Chinese, the idea is actually very simple, first through the index function in the strings library to get the byte position of the substring, and then through this position to get the byte array before the substring pre, and then convert the pre to []rune, get [] The length of the rune is the length of the string before the substring, that is, the character position of the substring in the string, with the following code:


Func Unicodeindex (str,substr string) int {  //substring in byte position of string  result: = Strings. Index (STR,SUBSTR)    if result >= 0 {    //Get string before substring and convert to []byte    prefix: = []byte (str) [0:result]  // Converts the string before the substring to []rune    rs: = []rune (string (prefix))  //Gets the length of the string before the substring, which is the character position of the substring in the string    result = Len (rs)  }    return result}


Note that the string is used here. The index function, similarly, can also be written in Chinese strings similar to strings in Indexany,lastindex and other functions


The same idea, I also wrote a function to intercept Chinese strings, as follows:


Func SubString (str string,begin,length int) (substr string) {  //converts the string into []rune  rs: = []rune (str)  lth: = Len ( RS)    //simple cross-border judgment  if begin < 0 {    begin = 0  }  if begin >= Lth {    begin = Lth  }  End: = Be Gin + length  if End > lth {    end = Lth   }    //returns SUBSTRING return  string (Rs[begin:end])}


If there is any mistake, please correct me.

Related Article

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.