ShortenString added
parent
8aac271e42
commit
23eada7730
@ -0,0 +1,28 @@
|
||||
/**
|
||||
= 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"
|
||||
|
||||
// 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 {
|
||||
|
||||
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
|
||||
}
|
Loading…
Reference in New Issue