str

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2023 License: MIT Imports: 4 Imported by: 0

README

goutils/str

The str package is a part of goutils library.

It provides utility functions for string manipulation in Go.


Functions

GenRandom
func GenRandom(length int) (string, error)

Generates a random string of the specified length. This function takes an integer input representing the length and returns a string of hexadecimal characters. If the generation fails, an error is returned.

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

Checks if a specific string exists in a given slice. It returns true if the string is found and false otherwise.

IsNumeric
func IsNumeric(s string) bool

Checks if a given string is composed entirely of numeric characters (0-9).

ToInt64
func ToInt64(value string) (int64, error)

Converts a string to an int64 value. If the conversion fails, an error is returned.

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

Splits a given string into a slice based on the provided delimiter.

SlicesEqual
func SlicesEqual(a, b []string) bool

Compares two string slices for equality. It returns true if the slices have the same length and contain the same strings in the same order. It returns false otherwise.


Installation

To use the goutils/str package, you need to install it via go get:

go get github.com/l50/goutils/str

Usage

After installation, you can import it in your project:

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

Tests

To run the tests for the goutils/str package, navigate to your $GOPATH/src/github.com/l50/goutils/str directory and run go test:

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

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 the specified length.

Parameters:

length: The length of the random string to be generated.

Returns:

string: The generated random string. error: An error if the random string generation fails.

Example:

str, err := GenRandom(10)

if err != nil {
  log.Fatalf("failed to generate random string: %v", err)
}

log.Printf("Generated random string: %s\n", str)

func InSlice

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

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

Parameters:

strToFind: The string to search for in the slice. inputSlice: The slice of strings to be searched.

Returns:

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

Example:

slice := []string{"apple", "banana", "cherry"} isFound := InSlice("banana", slice)

if isFound {
  log.Println("Found the string in the slice.")
}

func IsNumeric

func IsNumeric(s string) bool

IsNumeric checks if a given string is composed entirely of numeric characters (0-9). It iterates over each character in the string and checks if it can be converted to an integer without error. If all characters are numeric, the function returns true; otherwise, it returns false.

Parameters:

s: A string to check for numeric characters.

Returns:

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

Example:

str := "1234" isNum := str.IsNumeric(str)

if !isNum {
  log.Printf("The string %s is not numeric.\n", str)
}

func SlicesEqual

func SlicesEqual(a, b []string) bool

SlicesEqual compares two slices of strings for equality.

Parameters:

a: The first string slice to be compared. b: The second string slice to be compared.

Returns:

bool: true if the slices are equal (same length and same values), false otherwise.

Example:

a := []string{"apple", "banana", "cherry"} b := []string{"apple", "banana", "cherry"} isEqual := SlicesEqual(a, b)

if isEqual {
  log.Println("The string slices are equal.")
} else {

  log.Println("The string slices are not equal.")
}

func ToInt64

func ToInt64(value string) (int64, error)

ToInt64 converts a string to an int64.

Parameters:

value: The string to be converted to int64.

Returns:

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

Example:

num, err := ToInt64("1234567890")

if err != nil {
  log.Fatalf("failed to convert string to int64: %v", err)
}

log.Printf("Converted string to int64: %d\n", num)

func ToSlice

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

ToSlice converts a string to a slice of strings, using the specified delimiter.

Parameters:

delimStr: The string to be split into a slice. delim: The delimiter to be used for splitting the string.

Returns:

[]string: A slice of strings obtained by splitting the input string.

Example:

slice := ToSlice("apple,banana,cherry", ",")

for _, str := range slice {
  log.Println(str)
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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