insert string

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
5 posts Page 1 of 1
Contributors
User avatar
XTechVB
VIP - Site Partner
VIP - Site Partner
Posts: 727
Joined: Thu May 20, 2010 10:32 am

insert string
XTechVB
i have a richtextbox with the following lines
Code: Select all
test 1
test 2
test 3

hello 1
hello 2
hello 3
and so on

i need a way to insert this text "stop" after the last "test" line
is this possible?
Last edited by XTechVB on Sat May 12, 2012 5:54 pm, edited 1 time in total.
You can find me on Facebook or on Skype mihai_92b
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

Re: insert string
comathi
If you're talking about inserting the word "stop" on a new line after the text you have here, you could try this:
Code: Select all
RichTextBox1.Text=RichTextBox1.Text & vbNewLine & "stop"
If not, I'm not sure what you were asking for :lol:
User avatar
XTechVB
VIP - Site Partner
VIP - Site Partner
Posts: 727
Joined: Thu May 20, 2010 10:32 am

Re: insert string
XTechVB
no sorry i wasn't clear read the message again
You can find me on Facebook or on Skype mihai_92b
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Re: insert string
MrAksel
Code: Select all
Imports System.Text.RegularExpressions
'

'
Dim Matches As MatchCollection = Regex.Matches(RichTextBox1.Text, "test \d", RegexOptions.Multiline Or RegexOptions.IgnoreCase)
Dim Index As Integer = Matches(Matches.Length - 1).Index + Matches(Matches.Length - 1).Length
RichTextBox1.Text = RichTextBox1.Text.Insert(Index, vbNewLine + "Stop")
I think that will work, but only with tests up to "test 9". If you need more, like "test 99" you need to replace "\d" with "\d{2}", or for "test 999" you would replace with "\d{3}".
Last edited by MrAksel on Sun May 13, 2012 10:31 am, edited 1 time in total.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: insert string
mandai
If you don't want to hard code it so much you can use this:
Code: Select all
        Dim starting As Integer = RichTextBox1.Text.LastIndexOf(vbLf & "test ")
        Dim index As Integer = RichTextBox1.Text.IndexOf(vbLf, starting + 1)

        If index > -1 Then
            RichTextBox1.Text = RichTextBox1.Text.Insert(index, " stop")
        End If
5 posts Page 1 of 1
Return to “Coding Help & Support”