Sobes.tech
Junior — Middle

Kas saaksite nimetada teisi meetodeid stringide töötlemise rakendamiseks programmeerimises?

sobes.tech AI

Vastus AI-lt

In Go, there are several ways to work with strings, depending on the task:

  • Use standard functions from the strings package (e.g., strings.Split, strings.Replace, strings.Contains).
  • Convert the string into a byte slice ([]byte) for byte-by-byte processing.
  • Use rune for working with Unicode characters.
  • Use regular expressions via the regexp package for complex search and replace.

Example: counting the number of words in a string using strings.Fields:

package main

import (
    "fmt"
    "strings"
)

func main() {
    text := "Example string for processing"
    words := strings.Fields(text) // splits by spaces
    fmt.Println("Number of words:", len(words))
}