mirror of
https://github.com/ManyakRus/starter.git
synced 2025-11-28 23:20:10 +02:00
сделал StringSplitBylength()
This commit is contained in:
@@ -1782,3 +1782,23 @@ func RoundFloat64(value float64, precision int) float64 {
|
|||||||
ratio := math.Pow(10, float64(precision))
|
ratio := math.Pow(10, float64(precision))
|
||||||
return math.Round(value*ratio) / ratio
|
return math.Round(value*ratio) / ratio
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StringSplitBylength - разбивает строку на подстроки по n символов, с учётом рун
|
||||||
|
func StringSplitBylength(s string, n int) []string {
|
||||||
|
sub := ""
|
||||||
|
subs := []string{}
|
||||||
|
|
||||||
|
runes := bytes.Runes([]byte(s))
|
||||||
|
l := len(runes)
|
||||||
|
for i, r := range runes {
|
||||||
|
sub = sub + string(r)
|
||||||
|
if (i+1)%n == 0 {
|
||||||
|
subs = append(subs, sub)
|
||||||
|
sub = ""
|
||||||
|
} else if (i + 1) == l {
|
||||||
|
subs = append(subs, sub)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return subs
|
||||||
|
}
|
||||||
|
|||||||
@@ -1535,3 +1535,11 @@ func TestRoundFloat64(t *testing.T) {
|
|||||||
t.Errorf("Expected 1.1, but got %f", Otvet)
|
t.Errorf("Expected 1.1, but got %f", Otvet)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStringSplitBylength(t *testing.T) {
|
||||||
|
Text1 := "Привет мир"
|
||||||
|
MassOtvet := StringSplitBylength(Text1, 5)
|
||||||
|
if MassOtvet[0] != "Приве" {
|
||||||
|
t.Errorf("Expected 'Приве', but got %s", MassOtvet[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user