mirror of
https://github.com/ManyakRus/starter.git
synced 2025-11-26 23:10:42 +02:00
сделал StringSplitBylength_WithLastWord()
This commit is contained in:
@@ -1802,3 +1802,49 @@ func StringSplitBylength(s string, n int) []string {
|
|||||||
|
|
||||||
return subs
|
return subs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StringSplitBylength_WithLastWord - разбивает строку на подстроки по n символов, с учётом рун
|
||||||
|
func StringSplitBylength_WithLastWord(s string, n int, LastWord rune) []string {
|
||||||
|
Otvet := make([]string, 0)
|
||||||
|
|
||||||
|
runes := bytes.Runes([]byte(s))
|
||||||
|
for {
|
||||||
|
Otvet1, pos1 := stringSplitBylength_WithLastWord1(runes, n, LastWord)
|
||||||
|
Otvet = append(Otvet, string(Otvet1))
|
||||||
|
if len(runes) >= pos1 {
|
||||||
|
runes = runes[pos1:]
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(runes) <= 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Otvet
|
||||||
|
}
|
||||||
|
|
||||||
|
// stringSplitBylength_WithLastWord1 - возвращает первые n строк, заканчивая на LastWord
|
||||||
|
func stringSplitBylength_WithLastWord1(s []rune, n int, LastWord rune) ([]rune, int) {
|
||||||
|
Otvet := make([]rune, 0)
|
||||||
|
pos1 := 0
|
||||||
|
runes := s
|
||||||
|
length1 := len(runes)
|
||||||
|
length := MinInt(n, length1)
|
||||||
|
Otvet = runes[:length]
|
||||||
|
|
||||||
|
for i := length; i > 0; i-- {
|
||||||
|
if runes[i-1] == LastWord {
|
||||||
|
pos1 = i
|
||||||
|
Otvet = Otvet[:pos1]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if pos1 == 0 {
|
||||||
|
pos1 = len(Otvet)
|
||||||
|
}
|
||||||
|
|
||||||
|
return Otvet, pos1
|
||||||
|
}
|
||||||
|
|||||||
@@ -1543,3 +1543,11 @@ func TestStringSplitBylength(t *testing.T) {
|
|||||||
t.Errorf("Expected 'Приве', but got %s", MassOtvet[0])
|
t.Errorf("Expected 'Приве', but got %s", MassOtvet[0])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStringSplitBylength_WithLastWord(t *testing.T) {
|
||||||
|
Text1 := "Привет мир"
|
||||||
|
MassOtvet := StringSplitBylength_WithLastWord(Text1, 5, ' ')
|
||||||
|
if MassOtvet[0] != "Приве" {
|
||||||
|
t.Errorf("Expected 'Приве', but got %s", MassOtvet[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user