Skip to main content

string - Plugin operators

This page lists all operators available in the strings sub-package of ro.

Help improve this documentation

This documentation is still new and evolving. If you spot any mistakes, unclear explanations, or missing details, please open an issue.

Your feedback helps us improve!

Installโ€‹

First, import the sub-package in your project:

go get -u github.com/samber/ro/plugins/strings
  • Converts string to camel case.

    import (
    "github.com/samber/ro"
    rostrings "github.com/samber/ro/plugins/strings"
    )

    obs := ro.Pipe[string, string](
    ro.Just("hello_world_world"),
    rostrings.CamelCase[string](),
    )

    sub := obs.Subscribe(ro.PrintObserver[string]())
    defer sub.Unsubscribe()

    // Next: helloWorldWorld
    // Completed

    CamelCaseWithLanguage

    Converts the string to camel case using locale-aware casing.

    import (
    "github.com/samber/ro"
    rostrings "github.com/samber/ro/plugins/strings"
    "golang.org/x/text/language"
    )

    obs := ro.Pipe[string, string](
    ro.Just("Istanbul city"),
    rostrings.CamelCaseWithLanguage[string](language.Turkish),
    )

    sub := obs.Subscribe(ro.PrintObserver[string]())
    defer sub.Unsubscribe()

    // Next: ฤฑstanbulCity
    // Completed
    Prototypes:
    func CamelCase[T ~string]()
    func CamelCaseWithLanguage[T ~string](tag language.Tag)
  • Converts each string emitted by the source Observable to PascalCase (UpperCamelCase).

    import (
    "github.com/samber/ro"
    rostrings "github.com/samber/ro/plugins/strings"
    )

    obs := ro.Pipe[string, string](
    ro.Just("hello_world", "foo-bar", "some string"),
    rostrings.PascalCase[string](),
    )

    sub := obs.Subscribe(ro.PrintObserver[string]())
    defer sub.Unsubscribe()

    // Next: HelloWorld
    // Next: FooBar
    // Next: SomeString
    // Completed

    PascalCaseWithLanguage

    Converts the string to pascal case using locale-aware casing.

    import (
    "github.com/samber/ro"
    rostrings "github.com/samber/ro/plugins/strings"
    "golang.org/x/text/language"
    )

    obs := ro.Pipe[string, string](
    ro.Just("istanbul city"),
    rostrings.PascalCaseWithLanguage[string](language.Turkish),
    )

    sub := obs.Subscribe(ro.PrintObserver[string]())
    defer sub.Unsubscribe()

    // Next: ฤฐstanbulCity
    // Completed
    Prototypes:
    func PascalCase[T ~string]()
    func PascalCaseWithLanguage[T ~string](tag language.Tag)
  • Capitalizes first letter of string.

    import (
    "github.com/samber/ro"
    rostrings "github.com/samber/ro/plugins/strings"
    )

    obs := ro.Pipe[string, string](
    ro.Just("hello world"),
    rostrings.Capitalize[string](),
    )

    sub := obs.Subscribe(ro.PrintObserver[string]())
    defer sub.Unsubscribe()

    // Next: Hello world
    // Completed

    CapitalizeWithLanguage

    Capitalizes the first letter using locale-aware casing.

    import (
    "github.com/samber/ro"
    rostrings "github.com/samber/ro/plugins/strings"
    "golang.org/x/text/language"
    )

    obs := ro.Pipe[string, string](
    ro.Just("istanbul"),
    rostrings.CapitalizeWithLanguage[string](language.Turkish),
    )

    sub := obs.Subscribe(ro.PrintObserver[string]())
    defer sub.Unsubscribe()

    // Next: ฤฐstanbul
    // Completed
    Prototypes:
    func Capitalize[T ~string]()
    func CapitalizeWithLanguage[T ~string](tag language.Tag)
  • Converts string to kebab case.

    import (
    "github.com/samber/ro"
    rostrings "github.com/samber/ro/plugins/strings"
    )

    obs := ro.Pipe[string, string](
    ro.Just("HelloWorldTest"),
    rostrings.KebabCase[string](),
    )

    sub := obs.Subscribe(ro.PrintObserver[string]())
    defer sub.Unsubscribe()

    // Next: hello-world-test
    // Completed

    KebabCaseWithLanguage

    Converts the string to kebab case using locale-aware casing.

    import (
    "github.com/samber/ro"
    rostrings "github.com/samber/ro/plugins/strings"
    "golang.org/x/text/language"
    )

    obs := ro.Pipe[string, string](
    ro.Just("IstanbulCity"),
    rostrings.KebabCaseWithLanguage[string](language.Turkish),
    )

    sub := obs.Subscribe(ro.PrintObserver[string]())
    defer sub.Unsubscribe()

    // Next: ฤฑstanbul-city
    // Completed
    Prototypes:
    func KebabCase[T ~string]()
    func KebabCaseWithLanguage[T ~string](tag language.Tag)
  • Truncates string to length with ellipsis.

    import (
    "github.com/samber/ro"
    rostrings "github.com/samber/ro/plugins/strings"
    )

    obs := ro.Pipe[string, string](
    ro.Just("This is a very long string"),
    rostrings.Ellipsis[string](10),
    )

    sub := obs.Subscribe(ro.PrintObserver[string]())
    defer sub.Unsubscribe()

    // Next: This is...
    // Completed
    Prototype:
    func Ellipsis[T ~string](length int)
  • Converts string to snake case.

    import (
    "github.com/samber/ro"
    rostrings "github.com/samber/ro/plugins/strings"
    )

    obs := ro.Pipe[string, string](
    ro.Just("HelloWorldWorld"),
    rostrings.SnakeCase[string](),
    )

    sub := obs.Subscribe(ro.PrintObserver[string]())
    defer sub.Unsubscribe()

    // Next: hello_world_world
    // Completed

    SnakeCaseWithLanguage

    Converts the string to snake case using locale-aware casing.

    import (
    "github.com/samber/ro"
    rostrings "github.com/samber/ro/plugins/strings"
    "golang.org/x/text/language"
    )

    obs := ro.Pipe[string, string](
    ro.Just("IstanbulCity"),
    rostrings.SnakeCaseWithLanguage[string](language.Turkish),
    )

    sub := obs.Subscribe(ro.PrintObserver[string]())
    defer sub.Unsubscribe()

    // Next: ฤฑstanbul_city
    // Completed
    Prototypes:
    func SnakeCase[T ~string]()
    func SnakeCaseWithLanguage[T ~string](tag language.Tag)
  • Splits string into words.

    import (
    "fmt"
    "github.com/samber/ro"
    rostrings "github.com/samber/ro/plugins/strings"
    )

    obs := ro.Pipe[string, []string](
    ro.Just("hello world from go"),
    rostrings.Words[string](),
    )

    sub := obs.Subscribe(ro.NewObserver(
    func(words []string) {
    fmt.Printf("Next: %v\n", words)
    },
    func(err error) {
    fmt.Printf("Error: %v\n", err)
    },
    func() {
    fmt.Println("Completed")
    },
    ))
    defer sub.Unsubscribe()

    // Next: [hello world from go]
    // Completed
    Prototype:
    func Words[T ~string]()