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

сделал InsertTextFrom()

This commit is contained in:
Nikitin Aleksandr
2024-11-18 13:36:44 +03:00
parent 17dc9ee496
commit 8622ec873d
2 changed files with 46 additions and 0 deletions

View File

@@ -1244,3 +1244,25 @@ func IntNot0(MassInt ...int) int {
return Otvet
}
// InsertTextFrom - вставляет текст в середину строки
func InsertTextFrom(Text string, TextAdd string, IndexFrom int) string {
var buffer bytes.Buffer
//
if IndexFrom >= len(Text) {
return Text + TextAdd
}
//
if IndexFrom < 0 {
return TextAdd + Text
}
//
buffer.WriteString(Substring(Text, 0, IndexFrom))
buffer.WriteString(TextAdd)
buffer.WriteString(Substring(Text, IndexFrom, len(Text)))
return buffer.String()
}

View File

@@ -1248,3 +1248,27 @@ func TestIntNot0(t *testing.T) {
t.Errorf("Expected 3, but got %d", result3)
}
}
func TestInsertTextFrom_EndOfString(t *testing.T) {
expected := "Hello, World!"
result := InsertTextFrom("Hello, ", "World!", 7)
if result != expected {
t.Errorf("Expected %s, but got %s", expected, result)
}
}
func TestInsertTextFrom_BeginningOfString(t *testing.T) {
expected := "Hi, there"
result := InsertTextFrom(", there", "Hi", 0)
if result != expected {
t.Errorf("Expected %s, but got %s", expected, result)
}
}
func TestInsertTextFrom_MiddleOfString(t *testing.T) {
expected := "The quick brown fox jumps over the lazy dog."
result := InsertTextFrom("The quick fox jumps over the lazy dog.", "brown ", 10)
if result != expected {
t.Errorf("Expected %s, but got %s", expected, result)
}
}