Browse Source

Atoi without Error

master
Klaus Wendel 1 year ago
parent
commit
2a77c608be
  1. 17
      stringFunctions.go

17
stringFunctions.go

@ -9,12 +9,18 @@ Individual permissions beyond the scope of this license may be available at http
**/
package ebkTools
import "math"
import (
"math"
"strconv"
)
// ShortenString() shortens String in them MIDDLE and inserts a surrogate. It ist possible to prevent shortening, if maximum stringlength is inside a tolerance space
// fmt.Println(shortenString("abcdefghi",6,2,"[…]")) //a[…]hi
func ShortenString(input string, lengthmax, tolerance int, surrogate string) (output string) {
if len(input) > int(lengthmax+tolerance) && lengthmax != 0 {
if len(surrogate) == 0 {
surrogate = "[…]" //Set Default
}
charstodelete := (float64(lengthmax) / 2) - (float64(len(surrogate)) / 2)
front := input[0 : int(math.Floor(charstodelete))+1]
@ -26,3 +32,12 @@ func ShortenString(input string, lengthmax, tolerance int, surrogate string) (ou
return
}
// Atoi() without Error, returning integer only
func Atoi(s string) int {
i, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return i
}

Loading…
Cancel
Save