/** = Creative Commons Lizenzvertrag = ebktools von der archium GmbH, Gera ist lizenziert unter einer Creative Commons Namensnennung - Nicht kommerziell - Keine Bearbeitungen 4.0 International Lizenz. (http://creativecommons.org/licenses/by-nc-nd/4.0/deed.de) Individuelle über diese Lizenz hinausgehende Berechtigungen können Sie unter https://archium.org erhalten. = Creative Commons License = ebktools by archium GmbH, Gera is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. (http://creativecommons.org/licenses/by-nc-nd/4.0/) Individual permissions beyond the scope of this license may be available at https://archium.org. **/ package ebkTools 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] rear := input[len(input)-int(math.Ceil(charstodelete))-1:] output = front + surrogate + rear } else { output = input } return } // Atoi() without Error, returning integer only func Atoi(s string) int { i, err := strconv.Atoi(s) if err != nil { panic(err) } return i }