Sling Academy
Home/Golang/How to count words and characters in a string in Go

How to count words and characters in a string in Go

Last updated: November 28, 2024

Counting words and characters in a string is a common task in text processing. In this article, we'll look at how to achieve this in the Go programming language.

1. Counting Characters

To count the characters in a string, you can use the built-in function len() which returns the number of bytes in a string. For UTF-8 encoded strings where multi-byte characters are present, it's better to use the utf8.RuneCountInString function from the unicode/utf8 package.

package main

import (
    "fmt"
    "unicode/utf8"
)

func main() {
    str := "Hello, 世界"
    fmt.Println("Byte length:", len(str))
    fmt.Println("Character count:", utf8.RuneCountInString(str))
}

In this example, len(str) returns the number of bytes, whereas utf8.RuneCountInString(str) correctly counts the number of Unicode characters.

2. Counting Words

Counting words is a bit more involved, as you need to consider delimiters like spaces, punctuation, and other characters that separate words. Go doesn't have a direct function for counting words, but we can use the strings and unicode packages for this task.

package main

import (
    "fmt"
    "strings"
    "unicode"
)

func wordCount(s string) int {
    count := 0
    inWord := false
    for _, rune := range s {
        if unicode.IsSpace(rune) || unicode.IsPunct(rune) {
            inWord = false
        } else if !inWord {
            inWord = true
            count++
        }
    }
    return count
}

func main() {
    str := "Hello, 世界! Welcome to Go programming."
    fmt.Println("Word count:", wordCount(str))
}

This snippet defines a wordCount function that iterates through each rune in the string, counting words based on whitespace and punctuation as delimiters.

Conclusion

By leveraging Go's standard library, you can efficiently count characters and words. These techniques form the foundation for more advanced text processing tasks. With practice, you can build upon these concepts and apply them to various string manipulation challenges in Go.

Next Article: How to remove consecutive whitespace in a string in Go

Previous Article: Comprehensive Guide to Strings and Their Operations in Go

Series: Working with Strings in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant
  • Fixing Go error: syntax error: unexpected X, expecting Y