string_util.go 1.43 KB
package webutility

import (
	"fmt"
	"strings"
)

// IsWrappedWith ...
func IsWrappedWith(src, begin, end string) bool {
	return strings.HasPrefix(src, begin) && strings.HasSuffix(src, end)
}

// ParseInt64Arr ...
func ParseInt64Arr(s, sep string) (arr []int64) {
	s = strings.TrimSpace(s)
	if s != "" {
		parts := strings.Split(s, sep)
		arr = make([]int64, len(parts))
		for i, p := range parts {
			num := StringToInt64(p)
			arr[i] = num
		}
	}

	return arr
}

// Int64SliceToString ...
func Int64SliceToString(arr []int64) (s string) {
	for i, num := range arr {
		if i == 0 {
			s += fmt.Sprintf("%d", num)
		} else {
			s += fmt.Sprintf(",%d", num)
		}
	}

	return s
}

// CombineStrings ...
func CombineStrings(s1, s2, s3 string) string {
	s1 = strings.TrimSpace(s1)
	s2 = strings.TrimSpace(s2)

	if s1 != "" && s2 != "" {
		s1 += s3 + s2
	} else {
		s1 += s2
	}

	return s1
}

// ReplaceAny replaces any of the characters from patern found in s with r and returns a new resulting string.
func ReplaceAny(s, patern, r string) (n string) {
	n = s
	for _, c := range patern {
		n = strings.Replace(n, string(c), r, -1)
	}
	return n
}

func StringToBool(s string) bool {
	s = strings.ToLower(s)
	if s == "true" {
		return true
	}
	return false
}

func BoolToString(b bool) string {
	return fmt.Sprintf("%b", b)
}

func StringSliceContains(slice []string, s string) bool {
	for i := range slice {
		if slice[i] == s {
			return true
		}
	}
	return false
}