mirror of
https://github.com/ManyakRus/starter.git
synced 2025-11-26 23:10:42 +02:00
сделал InsertTextFrom()
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user