1
0
mirror of https://github.com/ManyakRus/starter.git synced 2025-11-28 23:20:10 +02:00

сделал StringSplitBylength()

This commit is contained in:
Nikitin Aleksandr
2025-05-27 11:57:49 +03:00
parent ffb9130fcd
commit d2d4378cde
2 changed files with 28 additions and 0 deletions

View File

@@ -1782,3 +1782,23 @@ func RoundFloat64(value float64, precision int) float64 {
ratio := math.Pow(10, float64(precision))
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
}

View File

@@ -1535,3 +1535,11 @@ func TestRoundFloat64(t *testing.T) {
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])
}
}