str

package
v2.2.6 Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: MIT Imports: 5 Imported by: 0

README

goutils/v2/str

The str package is a collection of utility functions designed to simplify common str tasks.


Table of contents


Functions

GenRandom(int)
GenRandom(int) string, error

GenRandom generates a random string of a specified length.

Parameters:

length: Length of the random string to be generated.

Returns:

string: Generated random string. error: An error if random string generation fails.


InSlice(string, []string)
InSlice(string, []string) bool

InSlice determines if a specified string exists in a given slice.

Parameters:

strToFind: String to search for in the slice. inputSlice: Slice of strings to be searched.

Returns:

bool: true if string is found in the slice, false otherwise.


IsNumeric(string)
IsNumeric(string) bool

IsNumeric checks if a string is entirely composed of numeric characters.

Parameters:

s: String to check for numeric characters.

Returns:

bool: true if the string is numeric, false otherwise.


SlicesEqual([]string)
SlicesEqual([]string) bool

SlicesEqual compares two slices of strings for equality.

Parameters:

a: First string slice for comparison. b: Second string slice for comparison.

Returns:

bool: true if slices are equal, false otherwise.


StripANSI(string)
StripANSI(string) string

StripANSI removes ANSI escape codes from a string.

Parameters:

str: String to remove ANSI escape codes from.

Returns:

string: String with ANSI escape codes removed.


ToInt64(string)
ToInt64(string) int64, error

ToInt64 converts a string to int64.

Parameters:

value: String to be converted to int64.

Returns:

int64: int64 equivalent of the string. error: An error if the conversion fails.


ToSlice(string, string)
ToSlice(string, string) []string

ToSlice converts a string to a slice of strings using a delimiter.

Parameters:

delimStr: String to be split into a slice. delim: Delimiter to be used for splitting the string.

Returns:

[]string: Slice of strings from the split input string.


Installation

To use the goutils/v2/str package, you first need to install it. Follow the steps below to install via go get.

go get github.com/l50/goutils/v2/str

Usage

After installation, you can import the package in your Go project using the following import statement:

import "github.com/l50/goutils/v2/str"

Tests

To ensure the package is working correctly, run the following command to execute the tests for goutils/v2/str:

go test -v

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.


License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenRandom

func GenRandom(length int) (string, error)

GenRandom generates a random string of a specified length.

**Parameters:**

length: Length of the random string to be generated.

**Returns:**

string: Generated random string. error: An error if random string generation fails.

Example
package main

import (
	"fmt"
	"log"

	"github.com/l50/goutils/v2/str"
)

func main() {
	randStr, err := str.GenRandom(10)
	if err != nil {
		log.Fatalf("failed to generate random string: %v", err)
	}
	fmt.Printf("Generated random string: %s\n", randStr)
}
Output:

func InSlice

func InSlice(strToFind string, inputSlice []string) bool

InSlice determines if a specified string exists in a given slice.

**Parameters:**

strToFind: String to search for in the slice. inputSlice: Slice of strings to be searched.

**Returns:**

bool: true if string is found in the slice, false otherwise.

Example
package main

import (
	"fmt"

	"github.com/l50/goutils/v2/str"
)

func main() {
	slice := []string{"apple", "banana", "cherry"}
	isFound := str.InSlice("banana", slice)
	fmt.Println(isFound)
}
Output:

true

func IsNumeric

func IsNumeric(s string) bool

IsNumeric checks if a string is entirely composed of numeric characters.

**Parameters:**

s: String to check for numeric characters.

**Returns:**

bool: true if the string is numeric, false otherwise.

Example
package main

import (
	"fmt"

	"github.com/l50/goutils/v2/str"
)

func main() {
	isNum := str.IsNumeric("1234")
	fmt.Println(isNum)
}
Output:

true

func SlicesEqual

func SlicesEqual(a, b []string) bool

SlicesEqual compares two slices of strings for equality.

**Parameters:**

a: First string slice for comparison. b: Second string slice for comparison.

**Returns:**

bool: true if slices are equal, false otherwise.

Example
package main

import (
	"fmt"

	"github.com/l50/goutils/v2/str"
)

func main() {
	a := []string{"apple", "banana", "cherry"}
	b := []string{"apple", "banana", "cherry"}
	isEqual := str.SlicesEqual(a, b)
	fmt.Println(isEqual)
}
Output:

true

func StripANSI added in v2.2.1

func StripANSI(str string) string

StripANSI removes ANSI escape codes from a string.

**Parameters:**

str: String to remove ANSI escape codes from.

**Returns:**

string: String with ANSI escape codes removed.

func ToInt64

func ToInt64(value string) (int64, error)

ToInt64 converts a string to int64.

**Parameters:**

value: String to be converted to int64.

**Returns:**

int64: int64 equivalent of the string. error: An error if the conversion fails.

Example
package main

import (
	"fmt"
	"log"

	"github.com/l50/goutils/v2/str"
)

func main() {
	num, err := str.ToInt64("1234567890")
	if err != nil {
		log.Fatalf("failed to convert string to int64: %v", err)
	}
	fmt.Printf("Converted string to int64: %d\n", num)
}
Output:

Converted string to int64: 1234567890

func ToSlice

func ToSlice(delimStr string, delim string) []string

ToSlice converts a string to a slice of strings using a delimiter.

**Parameters:**

delimStr: String to be split into a slice. delim: Delimiter to be used for splitting the string.

**Returns:**

[]string: Slice of strings from the split input string.

Example
package main

import (
	"fmt"

	"github.com/l50/goutils/v2/str"
)

func main() {
	slice := str.ToSlice("apple,banana,cherry", ",")
	fmt.Println(slice)
}
Output:

[apple banana cherry]

Types

This section is empty.

Jump to

Keyboard shortcuts

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