Text

package module
v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2023 License: MIT Imports: 2 Imported by: 1

README

Go Go Report card GoDoc

golang-stringbuilder

A string builder that has similar capabilities as the one from C#. The goal is to have a straightforward API that lets you work with strings easily.

Install

To install the package, call:

go get -u github.com/linkdotnet/golang-stringbuilder

Next import the package:

import ( "github.com/linkdotnet/golang-stringbuilder" )

Quickstart

The API derives from the C# StringBuilder. You can easily append strings or single runes.

func main() {
	sb := Text.StringBuilder{}
	sb.Append("Hello")
	sb.Append(" ")
	sb.Append("World")
	fmt.Println(sb.ToString())
}

Also more advanced use cases where you want to insert an arbitrary word at an arbitrary position are possible.

sb := NewStringBuilderFromString("Hello World")
sb.Insert(5, " my dear")
output := sb.ToString() // Hello my dear World

The StringBuilder also implements the io.Writer interface so it can be use with fmt.Fprintf and friends:

s := &StringBuilder{}

for i := 3; i >= 1; i-- {
	fmt.Fprintf(s, "%d...", i)
}

s.Append("lift off")
fmt.PrintLn(s.ToString()) // Prints 3...2...1...lift off

Benchmark

Check out the implementation of the benchmark in the corresponding file. Here are some results:

goos: darwin
goarch: arm64
pkg: github.com/linkdotnet/golang-stringbuilder
BenchmarkStringBuilderConcat
BenchmarkStringBuilderConcat-10           343710              3394 ns/op            4352 B/op          5 allocs/op
BenchmarkStringConcat
BenchmarkStringConcat-10                 1000000              1113 ns/op            6720 B/op         24 allocs/op
BenchmarkGoStringBuilderConcat
BenchmarkGoStringBuilderConcat-10        4204142               278.4 ns/op          1448 B/op          6 allocs/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type StringBuilder

type StringBuilder struct {
	// contains filtered or unexported fields
}

func NewStringBuilder

func NewStringBuilder(initialCapacity int) *StringBuilder

Creates a new instance of the StringBuilder with preallocated array

func NewStringBuilderFromString added in v0.2.0

func NewStringBuilderFromString(text string) *StringBuilder

Creates a new instance of the StringBuilder with a preallocated text

func (*StringBuilder) Append

func (s *StringBuilder) Append(text string) *StringBuilder

Appends a text to the StringBuilder instance

func (*StringBuilder) AppendBool added in v0.8.0

func (s *StringBuilder) AppendBool(flag bool) *StringBuilder

Appends a single boolean to the StringBuilder instance

func (*StringBuilder) AppendInt added in v0.8.0

func (s *StringBuilder) AppendInt(integer int) *StringBuilder

Appends a single integer to the StringBuilder instance

func (*StringBuilder) AppendLine

func (s *StringBuilder) AppendLine(text string) *StringBuilder

Appends a text and a new line character to the StringBuilder instance

func (*StringBuilder) AppendList added in v0.8.0

func (s *StringBuilder) AppendList(words []string) *StringBuilder

Appends a list of strings to the StringBuilder instance

func (*StringBuilder) AppendRune

func (s *StringBuilder) AppendRune(char rune) *StringBuilder

Appends a single character to the StringBuilder instance

func (*StringBuilder) AsRuneArray added in v0.9.0

func (s *StringBuilder) AsRuneArray() []rune

Returns the internal array of the string builder. Be careful as this returns the internal slice. Changes to that will reflect in this string builder instance.

func (*StringBuilder) AsRuneSlice added in v0.5.0

func (s *StringBuilder) AsRuneSlice() []rune

Returns the string builder as a rune-slice. Be careful as this returns the internal slice. Changes to that will reflect in this string builder instance.

func (*StringBuilder) Clear added in v0.2.0

func (s *StringBuilder) Clear()

Removes all characters from the current instance. This sets the internal size to 0. The internal array will stay the same.

func (*StringBuilder) FindAll added in v0.4.0

func (s *StringBuilder) FindAll(text string) []int

Returns all occurrences of the given text in the string builder. Returns an empty if no occurrence found.

func (*StringBuilder) FindFirst added in v0.4.0

func (s *StringBuilder) FindFirst(text string) int

Returns the first occurrence of the given text in the string builder. Returns -1 if not found

func (*StringBuilder) FindLast added in v0.4.0

func (s *StringBuilder) FindLast(text string) int

Returns the last occurrence of the given text in the string builder. Returns -1 if not found

func (*StringBuilder) Insert

func (s *StringBuilder) Insert(index int, text string) error

func (*StringBuilder) Len

func (s *StringBuilder) Len() int

Returns the current length of the represented string

func (*StringBuilder) Remove

func (s *StringBuilder) Remove(start int, length int) error

func (*StringBuilder) Replace added in v0.5.0

func (s *StringBuilder) Replace(oldValue string, newValue string) *StringBuilder

Replaces all occurrences of oldValue with newValue

func (*StringBuilder) ReplaceRune added in v0.5.0

func (s *StringBuilder) ReplaceRune(oldValue rune, newValue rune) *StringBuilder

Replaces all occurrences of oldValue with newValue

func (*StringBuilder) Reverse added in v0.10.0

func (s *StringBuilder) Reverse() *StringBuilder

Reverses the characters of a string builder

func (*StringBuilder) RuneAt added in v0.3.0

func (s *StringBuilder) RuneAt(index int) rune

Gets the rune at the specific position

func (*StringBuilder) SetRuneAt added in v0.11.0

func (s *StringBuilder) SetRuneAt(index int, val rune) error

Sets the rune at the specific position

func (*StringBuilder) Substring added in v0.10.0

func (s *StringBuilder) Substring(start, end int) (string, error)

Returns a substring from start (inclusive) to end (exclusive).

func (*StringBuilder) ToString

func (s *StringBuilder) ToString() string

Returns the represented string

func (*StringBuilder) Trim added in v0.9.0

func (s *StringBuilder) Trim(chars ...rune) *StringBuilder

Trims the given characters from the start and end of the string builder or all whitespaces if no characters are given

func (*StringBuilder) TrimEnd added in v0.9.0

func (s *StringBuilder) TrimEnd(chars ...rune) *StringBuilder

Trims the given characters from the start of the string builder or all whitespaces if no characters are given

func (*StringBuilder) TrimStart added in v0.9.0

func (s *StringBuilder) TrimStart(chars ...rune) *StringBuilder

Trims the given characters from the start of the string builder or all whitespaces if no characters are given

func (*StringBuilder) Write added in v0.6.0

func (s *StringBuilder) Write(p []byte) (int, error)

Implements the io.Writer interface so the StringBuilder can be used with fmt.Printf

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL