String

package module
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2022 License: MIT Imports: 26 Imported by: 0

README

Go Report Card Build Status Coverage Status

String

Did you ever ask yourself why go has no Object for string? Sou you can do stuff like:

var s String = "Das" 
bl := s.Contains("s")

instead of:

s := "Das"
bl := strings.Contains(s,"s")

like in javascript for example. And i saw that String handling in go is not very comfortable. You need to do a lot of things yourself what could be normal for a C developer but feels odd if you come from a javascript/php background. There are scenarios where an Object-Oriented approach is favorable in comparison to a functional one.

It feels more natural "to me" to use the function after the String the function uses. Readibility "for me" is much better like this. I benchmarked all fuctions in comparison from object-oriented to functional approach and didn't encounter any major performance deficits. You can run the benchmarks yourself:

go test -bench=.

All functions from the golang "strings" package were added

so this works:

s := String("That") 
if (s.Contains("at")) {
  // do some
}

In addition I added some comfort functions which you can see below. If you have any more ideas what one would need as functions in string processing which should be added please let me know. I will add them with Joy! Because Go is a wonderful language. And it could even be better if we work on smoothing out the edges so it gets more accessible.

To use the library the right way please import it with a dot in front so it acts like a local library:
import (
	."String"
)

otherwise you have to write "String." in front of all declarations which is not favorable.

String (Big capital S) is of type string so you can do anything with it you can do with a string. + and [] work as expected. If you want to use it with other functions don't forget to change the variable type before with string() or s.Tostring().

Functions which are not part of the main golang strings package but add to the functionality and comfort. Functions were only tested on Linux:

Substr(start, end int) String

A normal substring function utf-8 compatible.

var s String = "Hello world!"
var ss = s.Substr(3,5);

result: ss="lo wo"

ASCIIsubstr(start, end int) String

Same as subtring but only for ASCII Strings => is much faster if you only use ASCII.

ParseDateLocal(format String,location String) time.Time

Date parsing in Golang is freaky. I didn't like it. So wrote it it works more like normally.

Like this:

s := String("2009-11-17T20:34:58.651387237+01:00",
format:= String("YYYY-MM-DDThh:mm:ss.999999999Z:Z")
timeinBerlin := s.ParseDateLocal(format,"Europe/Berlin")        

Parses local date string into time with normally used syntax:

YY: last two digits of year.

YYYY: full Year.

M: 1 digit month.

MM: two digit month.

MMM: three letter month.

MMMM: full name month

D: 1 digit day.

DD: two digit day.

DDD: three letter day.

DDDD: full name day.

hh: hours 24

hh12: hours 12

m: single digit minutes

mm: double digit minutes

s: single digit seconds

ss: double digit seconds

9 stays for milliseconds

Z as golang Z07

ZZ as golang Z0700

ZZZ as golang Z070000

Z:Z as golang Z07:00

Z:Z:Z as golang Z07:00:00

if you need a minus before the Z's just add one.

Md5() String

Generates a md5 String out of provided String.

var s String = "The fog is getting thicker!And Leon's getting laaarger!"
var b = s.Md5();

result: b="e2c569be17396eca2a2e3c11578123ed"

Sha1() String

Generates a sha1 String out of provided String.

var s String = "This is an example sentence!"
var b = s.Sha1();

result: b="8b7d314a11b489238e9c8f07b117830b0e823a4a"

AesEncrypt(key String) String

Creates an Aes Encrypted String with provided key. Uses CTR as encryption algorith.

####128-bit:####

var s String = "Be good to people and people will be good to you"
s = s.AesEncrypt("52cb693d7e8ff8fecb2d9bee9653954b");
s = s.AesDecrypt("52cb693d7e8ff8fecb2d9bee9653954b");

result: s="Be good to people and people will be good to you"

####192-bit:####

var s String = "Be good to people and people will be good to you"
s = s.AesEncrypt("7eabf4e67aa790dba95ef3fd99f87613f1c0741e1d915ea8");
s = s.AesDecrypt("7eabf4e67aa790dba95ef3fd99f87613f1c0741e1d915ea8");

result: s="Be good to people and people will be good to you"

####265-bit:####

var s String = "Be good to people and people will be good to you"
s = s.AesEncrypt("57bcd105c2230065fcdd8ff312c201cdb896e28fa0967be2e2c43d61e7b7409c");
s = s.AesDecrypt("57bcd105c2230065fcdd8ff312c201cdb896e28fa0967be2e2c43d61e7b7409c");

result: s="Be good to people and people will be good to you"

AesEncryptByte(key String) []byte

Returns bytes instead of String. Same as AesEncrypt.

AesDecrypt(key String) String

Decrypts a AES String with provided key. See example above.

AesDecryptByte(key String) String

Please convert the []byte slice to String first. This will work!

GenerateAesKeyHex(length int)

Generate a random hex-key for Aes-decryption out of a String with the possible lengths of 16,24,32 bytes for 128, 192, 256-bit encryption.

import (
	."String"
	"fmt"
)

func main () {
	var s String
	s = s.GenerateAesKeyHex(16)
	fmt.Println(s.Tostring())
}
IsEmail() bool

Checks if String is an email.

var s String = "kh@kh.com"
bl = s.IsEmail;

result: bl=true

IsUrl() bool

Checks if String is an URL.

var url String = "https://google.de"
bl = url.IsUrl()

result: bl=true

IsWholeNumber() bool

Checks if String is a whole number.

var num String = "12345"
bl = num.IsWholeNumber()

result: bl=true

IsIpV4() bool

Checks if String is an IPV4 network adress.

var ip String = "192.168.0.1"
bl = ip.IsIpV4()

result: bl=true

IsIpV6() bool

Checks if String is an IPV6 network adress.

var ip String = "2a0a:a545:1728:0:1cb9:a3a8:42e1:d9ca"
bl = ip.IsIpV6()

result: bl=true

IsIp() bool

Checks if String is ip adress no matter if ipv4 or ipv6.

IsHtmlTag() bool

Checks if String is an html tag. Checks if String is an IPV6 network adress.

var tag String = "<html>"
bl = ip.IsHtmlTag()

result: bl=true

IsPhoneNumber() bool

Checks if String is a phone number.

var phonenumber String = "01776374859663"
bl = phonenumber.IsHtmlTag()

result: bl=true

IsFilePath() bool

Checks if String is a filepath.

var path String = "/home/user/go"
bl = path.IsFilePath()

result: bl=true

IsUserName (min int, max int) bool

Checks if String is a valid user name with min and max number of characters.

var user String = "k1ln"
bl = user.IsUserName(5,8)

result: bl=false

IsZipCode(country String) bool

Checks if String is ZipCode of provided country. Country should be provided in the countrycode-format => "DE => Germany, US => USA, FR => France etc..

var zip String = "50968"
bl = zip.IsZipCode("DE")

result: bl=true

IsIban(country String) bool

Checks if String is correct IBAN-Format doesn't check for 2-check digits at the beginning. Perhaps will add this in the future. Use country code for country like in IsZipCode.

var iban String = "DE89370400440532013000"
bl = iban.IsIban("DE")

result: bl=true

PwUpperCase (number int) bool

Checks if String contains number of uppercases.

var pw String = "PassWord"
bl = pw.PwUpperCase(2)

result: bl=true

PwSpecialCase(number int) bool

Checks if String contains number of special cases.

var pw String = "!PwSpecialCase?"
bl = pw.PwSpecialCase(2)

result: bl=true

PwDigits

Checks if String contains number of digits.

var pw String = "1PwDigits1"
bl = pw.PwSpecialCase(2)

result: bl=true

PwLowerCase(number int) bool

Checks if String contains number of lowercases.

var pw String = "PwLOWERcASE"
bl = pw.PwSpecialCase(2)

result: bl=true

Get() String

Get contents of a url address.

var url String = "http://google.de"
s := url.Get()
Json() map[String]interface{}

A very basic JSON parse function of a String. But there is something TODO here so better write your own. Only for very basic usage.

Open() String

Open File from provided String path and return String of file.

var path String = "/home/user/text.txt"
s := path.Open()
Exists() bool

Checks if String of path exists as url or exists as path on the system.

var path String = "/home/user/text.txt"
bl := path.Exists()

result: bl=true

var path String = "https://google.de"
bl := path.Exists()

result: bl=true

GetContents() String

Get String contents of file or url adress provided.

WriteToFile(path String)

Write String to provided path as file.

var str String = "Put this text into a file"
str.WriteToFile("/home/user/testfile.txt")
URLEncode() String

Encode String in URL-Format => Similar to Query Escape.

var str String = "uzgdauzgduaszd$&%$&%$&%$"
encoded := str.URLEncode()

result: encoded=uzgdauzgduaszd%24%26%25%24%26%25%24%26%25%24

URLDecode() String

Decode URL-encoded String.

var str String = "uzgdauzgduaszd%24%26%25%24%26%25%24%26%25%24"
encoded := str.URLEncode()

result: encoded=uzgdauzgduaszd$&%$&%$&%$

Post(url String, contenttype String) String

Basic Post with contenttype.

var str String = "{\"name\":\"K\"}"
var url := "http://httpbin.org/post"
var contenttype := "application/json"
str.Post(url,contenttype)

Execute() (String,String)

Executes a command in command line. Returns result in first String and error in second String.

var str String = "echo \"Dies +&& Jenes das\" \"bla&&\""
result,err := str.Execute()

result="Dies +&& Jenes das bla&&"

Php() (String,String)

Run String as php code and get result, error Strings back. You need php-cli installed.

var str String = "echo 'hello';"
result,err := str.Php()

result="hello"

Python() (String,String)

Run String in Python if Python installed. Returns result,error.

var str String = "print(\"hello\")"
result,err := str.Python()

result="hello"

Node() (String,String)

Run String in nodejs if nodejs is installed. Returns result,error.

var str String = "console.log(\"hello\")"
result,err := str.Node()

result="hello"

Perl() (String,String)

Run String in Perl if installed. Returns result,error.

var str String = "print \"hello\";"
result,err := str.Perl()

result="hello"

PhpFile() (String,String)

Run php file provided as path in String. Returns result,error.

PythonFile() (String,String)

Run python file provided as path in String. Returns result,error.

NodeFile() (String,String)

Run nodejs file provided as path in String. Returns result,error.

PerlFile() (String,String)

Run perl file provided as path in String. Returns result,error.

Int() int

Converts String to int.

var str String = "12345"
i := str.Int()

*result: i=12345

Int32() int32

Converts String to Int32.

Int64() int64

Converts String to Int64.

Uint32() uint32

Converts String to uint32.

Uint64() uint64

Converts String to uint64.

Bool() bool

Converts String to bool.

var str String = "true"
bl := str.Bool()

*result: bl=true

Float64() float64

Converts String to float64.

Float32() float32

Converts String to float32.

Uint() uint

Converts String to uint.

StripTags() String

Strips HTML-Tags from String.

var str String = "<html><body>This is some text in the body</body></html>"
str = str.StripTags()

*result: str="This is some text in the body"

Find(substring String) int

Find first appearance of substring in String.

var str String = "hello world"
ifind := str.Find("world")

*result: ifind=6

FindAll(substring String) []int

Find all appearances of substring in String.

var str String = "hello world world wrold world"
ifindarr := str.FindAll("world")

*result: ifindarr=[]int{6, 12, 24}

Left(length int) String

Get number of characters from the left of String.

var str String = "hello world"
left := str.Left(4)

*result: left="hell"

Right(length int) String

Get number of characters from the right of String.

var str String = "hello world"
right := str.Right(4)

*result: right="orld"

Reverse() String

Reverse the String. Why in the world you want that but you can do it.

var str String = "Kilian"
reverse := str.Reverse()

*result: reverse="nailiK"

WordCount() map[String]int

Count all Words in a String and return as ordered map.

var str String = "the the new new of da new the du new the of the of du"
arr := str.WordCount()

*result: arr=map[String]int{"the": 5, "new": 4, "of": 3, "du": 2, "da": 1}

RandomString(length int) String

Genrate a random String of length length. From seed "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

var str String 
randomstr := str.RandomString(12)
AddLeft(ss String) String

Add String to Left of String.

var str String = "world"
str = str.AddLeft("hello ")

*result: str="hello world"

AddRight(ss String) String

Add String right of String.

var str String = "hello "
str = str.AddRight("world")

*result: str="hello world"

AddPos(ss String,pos int) String

Add ss String to String at position pos.

var str String = "Elvis Presley"
str = str.AddPos("the one and only ",6)

*result: str="Elvis the one and only Presley"

FindInFiles(strpath String) Strings

Search for String in all files provided by path.

var str String = "needle"
arr := str.FindInFiles("/home/user/haystackfiles")

Documentation

Index

Constants

View Source
const RandomStringCharset = "abcdefghijklmnopqrstuvwxyz" +
	"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

Variables

View Source
var RandomStringseededRand *mrand.Rand = mrand.New(
	mrand.NewSource(time.Now().UnixNano()))
View Source
var Timeout = 5

Functions

func StringWithCharset

func StringWithCharset(length int, charset string) string

Types

type String

type String string

func (String) ASCIIsubstr

func (s String) ASCIIsubstr(start, end int) String

func (String) AddLeft

func (s String) AddLeft(ss String) String

func (String) AddPos

func (s String) AddPos(ss String, pos int) String

func (String) AddRight

func (s String) AddRight(ss String) String

func (String) AesDecrypt

func (s String) AesDecrypt(key String) String

needs base64encoded string

func (String) AesDecryptByte

func (s String) AesDecryptByte(key String) String

Convert byte to String object to use it. It will work

func (String) AesEncrypt

func (s String) AesEncrypt(key String) String

uses CTR

func (String) AesEncryptByte

func (s String) AesEncryptByte(key String) []byte

returns byte if you want to return binary

func (String) B64Decode

func (s String) B64Decode() String

func (String) B64Encode

func (s String) B64Encode() String

func (String) B64URLDecode

func (s String) B64URLDecode() String

func (String) B64URLEncode

func (s String) B64URLEncode() String

func (String) Bool

func (s String) Bool() bool

func (String) Compare

func (s String) Compare(b String) int

func (String) Contains

func (s String) Contains(substr String) bool

func (String) ContainsAny

func (s String) ContainsAny(chars String) int

func (String) ContainsRune

func (s String) ContainsRune(r rune) bool

func (String) Count

func (s String) Count(substr String) int

func (String) CreateCommandFields

func (s String) CreateCommandFields() []String

func (String) EqualFold

func (s String) EqualFold(t String) bool

func (String) Execute

func (s String) Execute() (String, String)

func (String) Exists

func (s String) Exists() bool

func (String) Fields

func (s String) Fields() Strings

func (String) FieldsFunc

func (s String) FieldsFunc(f func(rune) bool) []String

func (String) Find

func (s String) Find(substring String) int

func (String) FindAll

func (s String) FindAll(substring String) []int

func (String) FindInFiles

func (s String) FindInFiles(strpath String) Strings

func (String) Float32

func (s String) Float32() float32

func (String) Float64

func (s String) Float64() float64

func (String) GenerateAesKeyHex

func (s String) GenerateAesKeyHex(length int) String

generate 16 24 or 32 byte key for 128 192 or 256-bit Encryption

func (String) Get

func (s String) Get() String

func (String) GetContents

func (s String) GetContents() String

func (String) HasPrefix

func (s String) HasPrefix(prefix String) bool

func (String) HasSuffix

func (s String) HasSuffix(suffix String) bool

func (String) Index

func (s String) Index(substr String) int

func (String) IndexAny

func (s String) IndexAny(chars String) int

func (String) IndexByte

func (s String) IndexByte(c byte) int

func (String) IndexFunc

func (s String) IndexFunc(f func(rune) bool) int

func (String) IndexRune

func (s String) IndexRune(r rune) int

func (String) Int

func (s String) Int() int

func (String) Int32

func (s String) Int32() int32

func (String) Int64

func (s String) Int64() int64

func (String) IsEmail

func (s String) IsEmail() bool

func (String) IsFilePath

func (s String) IsFilePath() bool

func (String) IsHtmlTag

func (s String) IsHtmlTag() bool

func (String) IsIban

func (s String) IsIban(country String) bool

func (String) IsIbanFast

func (s String) IsIbanFast(country String) bool

func (String) IsIp

func (s String) IsIp() bool

func (String) IsIpV4

func (s String) IsIpV4() bool

func (String) IsIpV6

func (s String) IsIpV6() bool

func (String) IsPhoneNumber

func (s String) IsPhoneNumber() bool

func (String) IsUrl

func (s String) IsUrl() bool

func (String) IsUserName

func (s String) IsUserName(min int, max int) bool

func (String) IsWholeNumber

func (s String) IsWholeNumber() bool

func (String) IsZipCode

func (s String) IsZipCode(country String) bool

func (String) IsZipCodeFast

func (s String) IsZipCodeFast(country String) bool

func (String) Json

func (s String) Json() map[String]interface{}

TODO I need something better here

func (String) LastIndex

func (s String) LastIndex(substr String) int

func (String) LastIndexAny

func (s String) LastIndexAny(chars String) int

func (String) LastIndexByte

func (s String) LastIndexByte(c byte) int

func (String) LastIndexFunc

func (s String) LastIndexFunc(f func(rune) bool) int

func (String) Left

func (s String) Left(length int) String

func (String) Map

func (s String) Map(mapping func(rune) rune) String

func (String) Md5

func (s String) Md5() String

func (String) Node

func (s String) Node() (String, String)

func (String) NodeFile

func (s String) NodeFile() (String, String)

func (String) Open

func (s String) Open() String

func (String) OpenByte

func (s String) OpenByte() []byte

func (String) ParseDate

func (s String) ParseDate(format String) time.Time

func (String) ParseDateLocal

func (s String) ParseDateLocal(format String, location String) time.Time

func (String) Perl

func (s String) Perl() (String, String)

func (String) PerlFile

func (s String) PerlFile() (String, String)

func (String) Php

func (s String) Php() (String, String)

func (String) PhpFile

func (s String) PhpFile() (String, String)

func (String) Post

func (s String) Post(url String, contenttype String) String

func (String) PrecompileIsIbanFast

func (s String) PrecompileIsIbanFast(country String)

func (String) PrecompileIsZipCodeFast

func (s String) PrecompileIsZipCodeFast(country String)

func (String) PwDigits

func (s String) PwDigits(number int) bool

func (String) PwLowerCase

func (s String) PwLowerCase(number int) bool

func (String) PwSpecialCase

func (s String) PwSpecialCase(number int) bool

func (String) PwUpperCase

func (s String) PwUpperCase(number int) bool

func (String) Python

func (s String) Python() (String, String)

func (String) PythonFile

func (s String) PythonFile() (String, String)

func (String) RandomString

func (s String) RandomString(length int) String

func (String) Repeat

func (s String) Repeat(count int) String

func (String) Replace

func (s String) Replace(old, new String, n int) String

func (String) ReplaceAll

func (s String) ReplaceAll(old, new String) String

func (String) Reverse

func (s String) Reverse() String

func (String) Right

func (s String) Right(length int) String

func (String) Sha1

func (s String) Sha1() String

func (String) Split

func (s String) Split(sep String) []String

func (String) SplitAfter

func (s String) SplitAfter(sep String) []String

func (String) SplitAfterN

func (s String) SplitAfterN(sep String, n int) []String

func (String) SplitN

func (s String) SplitN(sep String, n int) []String

func (String) StripTags

func (s String) StripTags() String

func (String) Substr

func (s String) Substr(start, end int) String

func (String) Title

func (s String) Title() String

func (String) ToLower

func (s String) ToLower() String

func (String) ToLowerSpecial

func (s String) ToLowerSpecial(c unicode.SpecialCase) String

func (String) ToTitle

func (s String) ToTitle() String

func (String) ToTitleSpecial

func (s String) ToTitleSpecial(c unicode.SpecialCase) String

func (String) ToUpper

func (s String) ToUpper() String

func (String) ToUpperSpecial

func (s String) ToUpperSpecial(c unicode.SpecialCase) String

func (String) ToValidUTF8

func (s String) ToValidUTF8(replacement String) String

I dont know how to test this TODO

func (String) Tostring

func (s String) Tostring() string

func (String) Trim

func (s String) Trim(cutset String) String

func (String) TrimFunc

func (s String) TrimFunc(f func(rune) bool) String

func (String) TrimLeft

func (s String) TrimLeft(cutset String) String

func (String) TrimLeftFunc

func (s String) TrimLeftFunc(f func(rune) bool) String

func (String) TrimPrefix

func (s String) TrimPrefix(prefix String) String

func (String) TrimRight

func (s String) TrimRight(cutset String) String

func (String) TrimRightFunc

func (s String) TrimRightFunc(f func(rune) bool) String

func (String) TrimSpace

func (s String) TrimSpace() String

func (String) TrimSuffix

func (s String) TrimSuffix(suffix String) String

func (String) URLDecode

func (s String) URLDecode() String

func (String) URLEncode

func (s String) URLEncode() String

func (String) Uint

func (s String) Uint() uint

func (String) Uint32

func (s String) Uint32() uint32

func (String) Uint64

func (s String) Uint64() uint64

func (String) WordCount

func (s String) WordCount() map[String]int

func (String) WriteToFile

func (s String) WriteToFile(path String)

type Strings

type Strings []String

func (Strings) Join

func (s Strings) Join(sep String) String

type Strint

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

Jump to

Keyboard shortcuts

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