No, len() starts counting from 1.
How to find the length of a string in Golang
Key takeaways:
String length is essential for various applications, including UI design, text validation, and networking.
Methods:
len()andRuneCountInString()
len(): This returns the number of bytes in a string.
RuneCountInString(): This returns the number of characters in a string.
Understanding how to find the length of a string is a fundamental operation. It is difficult to think of a domain that doesn’t require string length at some point—whether in web development, data science, or user interface development, to name a few. Many applications rely on string length:
- User interface design: Ensure text fits within UI elements to avoid overflow.
- Text validation: Validate user input against length constraints (e.g., password limits).
- Networking: Frame network messages correctly by measuring byte length.
Finding the length of a Go string
Go by Griesemer et al. makes finding the length of a string process straightforward. In Golang, we use the following two methods to find Golang string length:
len()RuneCountInString()
Method 1: Use the len() method to find the length of a string
The len()function in Golang returns the number of bytes in a string.
Syntax
len(str)
Parameters
This method takes a string str as a parameter.
Return value
This method returns the length of the string.
Code example: Counting the byte length of a string in Golang
The following code demonstrates how to determine Golang string length.
package mainimport("fmt")//Program starts from herefunc main() {//provide the string herevar str = "educative"//calculate lengthvar length = len(str)//display lengthfmt.Println("Length of the string is :", length)}
Code explanation
- Line 8: We call the
main()function. - Line 10: We declare the string
str. - Line 13: We pass the string
stras a parameter to thelen()method. Next, we assign length to thelengthvariable, which is returned bylen(). - Line 16: We use the
println()function to print the length.
Method 2: Use the RuneCountInString() method to find the length of a string
The RuneCountInString() method is provided by the utf8 package.
Syntax
RuneCountInString(str)
Parameters
This function accepts a string as its parameter.
Return value
It returns the actual number of characters in the string.
Code example: Counting characters in a string in Golang
The following code demonstrates how to determine Golang string length.
package main//import utf8 packageimport ("unicode/utf8""fmt")//Program exection starts herefunc main() {//provide the string herevar str = "educative"//calculate lengthvar length = utf8.RuneCountInString(str)//display lengthfmt.Println("Length of the string is :", length)}
Code explanation
Most of the explanation is the same as for the len() method.
- Line 5: We import the
utf8package. - Line 15: We pass the string
stras a parameter toRuneCountInString(). We assign the output length to thelengthvariable.
How to decide which method to use
We have learned two ways to find the length of a string in Golang.
For normal characters, we can use either the len() function or the RuneCountInString() method. However, if the string contains special characters, such as the Black Chess Queen ♛, the len() function will return a length value that represents the number of bytes, not the number of characters. This can be misleading because each special character may occupy multiple bytes. In such cases, it is better to use RuneCountInString(), which accurately counts the number of characters, regardless of how many bytes they occupy.
package main//import utf8 packageimport ("unicode/utf8""fmt")//Program exection starts herefunc main() {//provide the string herevar str = "Black Chess Queen ♛"//calculate lengthvar length = utf8.RuneCountInString(str)//display lengthfmt.Println("Length of the string by RuneCountInString() is :", length)//display lengthfmt.Println("Length of the string by len() is :", len(str))}
Want to learn Go and unlock its full potential? The Way to Go course is your perfect starting point! This course will teach you Go's core constructs and advanced concepts like error-handling, networking, and templating and help you master efficient programming techniques. With hands-on projects and real-world examples, you’ll gain the skills to build robust, concurrent applications.
Start your journey to mastering Go today!
Frequently asked questions
Haven’t found what you were looking for? Contact Us
Does len() start from 0?
Does len() include spaces?
How do we find the length of a string in Java?
Can len() be used for strings with multi-byte characters?
How do we handle strings length with emojis in Golang?
Free Resources