Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a substring from a string of runes in golang?

Tags:

People also ask

How do I get the substring of a string in Golang?

In the Go strings, you are allowed to split the string after the specified separator using a SplitN() function. This function splits a slice into all substrings after each instance of the given separator and returns a slice of the substrings between those separators.

How do you check if a string starts with a substring in Golang?

In Golang strings, you can check whether the string begins with the specified prefix or not with the help of HasPrefix() function. This function returns true if the given string starts with the specified prefix and return false if the given string does not start with the specified prefix.

Does substring Go?

Go does not provide a substring() method. But with slice syntax, we take substrings based on the first and last index.


I found this, https://groups.google.com/forum/#!topic/golang-nuts/YyKlLwuWt3w but as far as I can tell, the solutions didn't work for me.

If you use the method of treating a string as a slice(str[:20]), it breaks off in the middle of characters and we get "ال�".

Edit: I believe I could write a function and do it as a multiple of 3's as runes are int32 (32bits/(8bits/byte)). I would first have to check if there are runes.