Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the last X Characters of a Golang String?

If I have the string "12121211122" and I want to get the last 3 characters (e.g. "122"), is that possible in Go? I've looked in the string package and didn't see anything like getLastXcharacters.

like image 854
sourcey Avatar asked Oct 02 '14 17:10

sourcey


People also ask

How do I get the last 5 characters of a string?

To get the last N characters of a string, call the slice method on the string, passing in -n as a parameter, e.g. str. slice(-3) returns a new string containing the last 3 characters of the original string. Copied! const str = 'Hello World'; const last3 = str.

What is the last character in a string of characters?

The end of the string is marked with a special character, the null character , which is simply the character with the value 0. (The null character has no relation except in name to the null pointer . In the ASCII character set, the null character is named NUL.)

How do you get the second to last character in a string?

To get the second to last character in a string, use bracket notation to access the string at index string. length - 2 , e.g. str[str. length - 2] . The last index in the string is str.

How do I get the last 5 characters of a string in Python?

The last character of a string has index position -1. So, to get the last character from a string, pass -1 in the square brackets i.e. It returned a copy of the last character in the string. You can use it to check its content or print it etc.

How to get the first X characters of a Golang string?

How to get the first X Characters of a Golang String? Use a slice expression on a string to get the section of the string that you want. package main import ( "fmt" ) func main () { str := "This is a string" fmt.Println( str) // get first 10 chars first10 := str [0:9] fmt.

How to get last 3 bytes of a string in Golang?

You can use a slice expression on a string to get the last three bytes. Check Strings, bytes, runes and characters in Go and Slice Tricks. Show activity on this post.

How to get the index of a string in Golang?

strings.LastIndex () function in the Golang returns the starting index of the occurrence of the last instance of a substring in a given string. If the substring is not found then it returns -1. Thus, this function returns an integer value. The index is counted taking zero as the starting index of the string.

How do I get the last 3 bytes of a string?

You can use a slice expression on a string to get the last three bytes. Check Strings, bytes, runes and characters in Go and Slice Tricks. Show activity on this post. The answer depends on what you mean by "characters". If you mean bytes then: You can also convert the string to a []rune and operate on the rune slice, but that allocates memory.


1 Answers

You can use a slice expression on a string to get the last three bytes.

s      := "12121211122" first3 := s[0:3] last3  := s[len(s)-3:] 

Or if you're using unicode you can do something like:

s      := []rune("世界世界世界") first3 := string(s[0:3]) last3  := string(s[len(s)-3:]) 

Check Strings, bytes, runes and characters in Go and Slice Tricks.

like image 76
OneOfOne Avatar answered Sep 20 '22 18:09

OneOfOne