Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to find the specified substring of go language string

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "go language string how to find the specified substring", the article explains the content is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "go language string how to find the specified substring" bar!

In the go language, you can use slicing with the "strings.Index ()" function to find the specified substring, which can search for another substring in the string to get the start and end position of the substring; the grammatical format is "string [start position + end position:]".

The operating environment of this tutorial: windows10 system, GO 1.11.2, Dell G3 computer.

Getting a certain substring of a string is a common operation in development. We usually call a certain segment of a character in a string a substring.

In the following example, the strings.Index () function is used to search for another substring in the string, as follows:

Tracer: = "death is coming, bye bye" comma: = strings.Index (tracer, ",") pos: = strings.Index (tracer [comma:], "death") fmt.Println (comma, pos, tracer [comma+pos:])

The program output is as follows:

12 3 bye bye of death

The code is as follows:

1) Line 2 attempts to search for a comma in Chinese in the string of tracer, and the returned position exists in the comma variable of type int, indicating the position of the ASCII code starting from the tracer string.

The strings.Index () function does not provide the ability to search from a certain offset, as in other languages. However, we can slice the string to implement this logic.

2) in line 4, tracer [comma:] constructs a substring from the comma position of tracer to the end of the tracer string, which is returned to string.Index () for re-indexing. The resulting pos is relative to tracer [comma:].

The position of the comma comma is 12, while pos is the relative position, with a value of 3. In order to get the position of the second "god of death", that is, the string after the comma, we must let comma add the relative offset of pos, calculate the offset of 15, and then calculate the final substring by slicing tracer [comma+pos:] to get the final result: "death bye bye".

Thank you for your reading, the above is the "go language string how to find the specified substring" content, after the study of this article, I believe you on the go language string how to find the designated substring of this problem has a deeper understanding, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report