0

I want to insert a word inside an existing word? Both are Strings.

For example:

Given String word:

HELLO SAMPLE SENTENCE

i want to insert the word I AM A so my output would be:

HELLO I AM A SAMPLE SENTENCE

i am inserting here basing on the word SAMPLE. So the insertion starts before the word SAMPLE. is this possible?

4

3 回答 3

3

根据您对逻辑的描述(没什么可继续的),我会使用:

Dim input As String = "HELLO SAMPLE SENTENCE"
Dim iSample As Integer = input.IndexOf("SAMPLE")
Dim output As String = input.Insert(iSample, "I AM A ")

这使用 BCL 函数 String.Insert,它只是将一个字符串插入到另一个字符串的特定位置。

于 2013-08-26T04:51:57.657 回答
1

创建一个这样的函数:

Function InsertBefore(sentence As String, find As String, textToInsert As String
    Return sentence.Replace(find, textToInsert+Find)
End Function

并这样称呼它:

sentence = InsertBefore("HELLO SAMPLE SENTENCE", " SAMPLE ", "I AM A")
于 2013-08-26T04:46:30.703 回答
-1

如果我没记错的话,你可以String.split()在你的字符串上使用这个函数。

请参阅DotNetPerls在此处拆分的页面。

您可以将字符串拆分为一个数组,然后将所需的行插入到数组中,然后使用String.Join()(感谢 Monty,我不再经常使用 Visual Basic,我忘记了 :))将它们重新连接在一起。

希望这有帮助:)

于 2013-08-26T04:45:24.117 回答