How do I manipulate the last string of a Richtextbox in Visual Basic - vb.net

I am trying to take a string in a rich text box and replace them with a different string.
Now how this should work is that if two same characters are entered into the text box
e.g tt the "tt" will be replaced with "Ǿt" , it adds back one of the t's to the replaced string. Only the most recently entered string is manipulated if two same characters are entered .
I read the LAST string that is in the RichTextBox by using this method
Dim laststring As String = RichTextBox1.Text.Split(" ").Last
'hitting space bar breaks the operation so if i enter t t there will be no replacement
this is the replacement method which I use , it works correctly .
if laststring = "tt"
RichTextBox1 .Text = RichTextBox1 .Text.Replace("tt", "Ǿt")
This method is inefficient because i need to check id there are double letters for all letters and if i was to use this method it would tavke up a lot of code .
how can I accomplish this using a shorter method??

You need to put the if then section in a loop.
Dim holdstring As String
Dim doubleinstance() As String = {"bb", "tt", "uu"} ' array
Dim curstring As String = RichTextBox1.Text.Split(" ").Last
For Each item As String In doubleinstance
If RichTextBox1.Text.EndsWith(item) Then
holdstring = RichTextBox1.Text.Split(" ").Last.Length - 1 ' change to subtract 1 character from doubleinstance
RichTextBox1.Text = RichTextBox1.Text.Replace(curstring, "Ǿt" & holdstring)
MsgBox(curstring)
End If
Next item

Here's a bit of code to get you in the right direction...
There are a couple of variations of .Find, but you probably want to look at the .Select method.
With RichTextBox1
.Find("Don")
.SelectedText = "Mr. Awesome"
End With

Here is a way I came up with
Dim holdstring As String
Dim doubleinstance() As String = {"bb", "tt", "uu"} ' array
Dim curstring As String = RichTextBox1.Text.Split(" ").Last
If curstring = doubleinstance(0) And RichTextBox1.Text.EndsWith(doubleinstance(0)) Then
holdstring = RichTextBox1.Text.Split(" ").Last.Length - 1 ' change to subtract 1 character from doubleinstance
RichTextBox1.Text = RichTextBox1.Text.Replace(curstring, "Ǿt" + holdstring)
MsgBox(curstring)
End If
where i have doubleinstance(0) how do i get the if statement to not only check a single index but all of the index from 0 to 2 in this example ?

Related

VB.NET - Delete excess white spaces between words in a sentence

I'm a programing student, so I've started with vb.net as my first language and I need some help.
I need to know how I delete excess white spaces between words in a sentence, only using these string functions: Trim, instr, char, mid, val and len.
I made a part of the code but it doesn't work, Thanks.
enter image description here
Knocked up a quick routine for you.
Public Function RemoveMyExcessSpaces(str As String) As String
Dim r As String = ""
If str IsNot Nothing AndAlso Len(str) > 0 Then
Dim spacefound As Boolean = False
For i As Integer = 1 To Len(str)
If Mid(str, i, 1) = " " Then
If Not spacefound Then
spacefound = True
End If
Else
If spacefound Then
spacefound = False
r += " "
End If
r += Mid(str, i, 1)
End If
Next
End If
Return r
End Function
I think it meets your criteria.
Hope that helps.
Unless using those VB6 methods is a requirement, here's a one-line solution:
TextBox2.Text = String.Join(" ", TextBox1.Text.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries))
Online test: http://ideone.com/gBbi55
String.Split() splits a string on a specific character or substring (in this case a space) and creates an array of the string parts in-between. I.e: "Hello There" -> {"Hello", "There"}
StringSplitOptions.RemoveEmptyEntries removes any empty strings from the resulting split array. Double spaces will create empty strings when split, thus you'll get rid of them using this option.
String.Join() will create a string from an array and separate each array entry with the specified string (in this case a single space).
There is a very simple answer to this question, there is a string method that allows you to remove those "White Spaces" within a string.
Dim text_with_white_spaces as string = "Hey There!"
Dim text_without_white_spaces as string = text_with_white_spaces.Replace(" ", "")
'text_without_white_spaces should be equal to "HeyThere!"
Hope it helped!

How To Remove Last Word With Slash In A Textbox VB.NET

I'm trying to remove the last word on a Label with it's slash without putting it in a String or an Array because the words are typed, looks something like this D:\Folder1\Folder2\Folder3\Folder4 and if i click a button it should remove the Folder4 and when i click the button again it should remove the Folder3 and so on.
Dim s As String = Label5.Text
Dim r As String = Replace(s, "\", "")
Label5.Text = r
And this only removes the slash, how do i add the texts?
I tried something like this:
Replace(s, "\" & Label5.Text.TrimEnd, "")
but nothing happens. Help?
The string class has a method called LastIndexOf that tells you the index of the last character that you pass as parameter.
Then the Substring method allows you to keep only the part before that index
' Label.Text = "D:\Folder1\Folder2\Folder3\Folder4"
Dim pos As Integer = Label.Text.LastIndexOf("\")
if pos <> -1 Then
Label.Text = Label.Text.Substring(0, pos)
End If
No need to split the input in an array and then rebuild your string.
Another method, very simple, (but that carries also an assumption) is to use the Path class
' Label.Text = "D:\Folder1\Folder2\Folder3\Folder4"
Label.Text = Path.GetDirectoryName(Label.Text)
The assumption, of course, is in the fact, that you have a correctly typed folder name (not necessary that the folder exists though)
Idea is, to find last word by splitting the string with \ and then finding last element in the splitted array
Dim s As String = Label5.Text '"D:\Folder1\Folder2\Folder3\Folder4"
Dim r As String = s.Replace("\" & s.Split("\")(s.Split("\").Length-1), String.Empty)
Label5.Text=r 'Console.Write(r)
Try this:
Dim r as String = Replace(s, s.Split("\").Last(), "")
.Last() will make sure you get the last word after the "\"

Replacing string at certain index of Split

Using streamreader to read line by line of a text file. When I get to a certain line (i.e., 123|abc|99999||ded||789), I want to replace ONLY the first empty area with text.
So far, I've been toying with
If sLine.Split("|")(3) = "" Then
'This is where I'm stuck, I want to replace that index with mmm
End If
I want the output to look like this: 123|abc|99999|mmm|ded||789
Considering you already have code determining if the "mmm" string needs to be added or not, you could use the following:
Dim index As Integer = sLine.IndexOf("||")
sLine = sLine.Insert(index + 1, "mmm")
You could split the string, modify the array and rejoin it to recreate the string:
Dim sLine = "123|abc|99999||ded||789"
Dim parts = sLine.Split("|")
If parts(3) = "" Then
parts(3) = "mmm"
sLine = String.Join("|", parts)
End If
I gather that if you find one or more empty elements, you want to replace the first empty element with data and leave the rest blank. You can accomplish this by splitting on the pipe to get an array of strings, iterate through the array and replace the first empty element you come across and exit the loop, and then rejoin your array.
Sub Main()
Dim data As String = "123||abc|99999||ded||789"
Dim parts = data.Split("|")
For index = 0 To parts.Length - 1
If String.IsNullOrEmpty(parts(index)) Then
parts(index) = "mmm"
Exit For
End If
Next
data = String.Join("|", parts)
Console.WriteLine(data)
End Sub
Results:
123|mmm|abc|99999||ded||789

Replace text in textbox without using replace function

This is more like a logical question, I just need an idea on how to best approach this problem.
I am trying to do this in visual basic. I have to text box, in text box 1, I would have some text and if it contains certain word, I want to replace that with something else and have the new output in text box 2, after clicking on a convert button.
I know this can be done really easily with the replace function built in, but I am trying to do it without the replace function.
For example:
I want to replace "fox" with "dog"
Textbox 1: The quick brown fox jumped over the lazy dog.
Textbox 2: The quick brown dog jumped over the lazy dog.
I got it to work with the code below, but this only works for the first instance. How can I change my code for all instances.
String from InputText
Dim S1 As String
S1 = InputText.Text
'Position of start of "Edge Computer"
Dim pStart As Integer
pStart = S1.IndexOf("Edge Computer")
'Position of end of "Edge Computer"
Dim pEnd As Integer
pEnd = S1.IndexOf("r", pStart) + 1
'Actual old name as string
Dim strOld As String
strOld = S1.Substring(pStart, 13)
'New String for Output
Dim S2 As String
S2 = S1.Substring(0, pStart) & "3dg3" & S1.Substring(pEnd)
OutputText.Text = S2
Thanks
If you don't want to use replace you could use substring to select the text before and after the word that as to be replaced.
You did not post any code so it's hard to put an example that would fit you better
Dim NewText as String
If textbox1.text.IndexOf("OldWord") > - 1 Then
NewText = textbox1.text.Substring(0, textbox1.text.IndexOf("OldWord")) & "NewWord " & textbox1.text.Substring(textbox1.text.IndexOf("OldWord") + "Something".length, textbox1.text.length )
'Make sure you replacfe OldWold and NewWord to the specific text or variable
End If
To make it work with all instance of the word, you just need to replace the if by a loop
Dim NewText as String
Dim OldText as String = textbox1.text
While OldText.IndexOf("OldWord") > - 1
NewText = OldText.Substring(0, OldText.IndexOf("OldWord")) & "NewWord " & OldText.Substring(OldText.IndexOf("OldWord") + "OldWord".length, OldText.length )
OldText = OldText.Substring(OldText.IndexOf("OldWord") + "OldWord".length, OldText.length)
'Make sure you replace OldWold and NewWord to the specific text or variable
End While
Let me know if this works or not

Display first and last half of any string entered into textbox

I want to display the first and last characters of any given string entered into a textbox. The strings can be of any length as the user wants (as long as it is one word) I would like to be able to do something like this... "william = will and iam" or "Celtic = Cel and tic"
I understand I would have to split or divide the string. How would I go about doing this? Any help is appreciated, thanks.
EDIT:
Thanks for your help once again guys, this is how the code ended up!
Dim strInput = txtString.Text
Dim halflength = strInput.Length / 2
Dim firsthalf = strInput.Substring(0, halflength)
Dim secondhalf = strInput.Substring(halflength)
Dim strResults = firsthalf
Dim secondResult = secondhalf
MessageBox.Show(firsthalf)
MessageBox.Show(secondhalf)
MessageBox.Show("First half of string contains... " & " " & strResults.Length.ToString & " characters", "Character Count")
MessageBox.Show("Second half of string contains... " & " " & secondResult.Length.ToString & " characters", "Character Count")
EDIT:
Also meant to mention my current incorrect code.
Dim strInput As String
Dim strLength As String
Dim strResults As String
strInput = txtString.Text
strLength = strInput.Length / 2
strResults = txtString.Text
MessageBox.Show(strInput.Length.ToString, "Length of characters")
MessageBox.Show(strLength.ToString)
MessageBox.Show(strResults.Substring(0, 3))
String.Substring and String.Length should give you everything you need to get started on this.
Seeing your existing code will make this easier. Let's walk through what we have now.
Let's assume we have just a plain, simple string like this instead of a textbox for the sake of making things easier:
Dim txtString = "Hello World"
Now, in order to split the length of the string in half; we need to get the length. The `Length property will give is that, and then divide it by two.
Dim halfLength = txtString.Length \ 2
This will perform integer division; so any remaining decimal is truncated.
Now we know where the middle of the string is. We can now use String.Substring to carve out a peice of the string by index. Substring takes two parameters, the index where to start the string, and number of characters to take. There is a second overload that takes the index to start at and consumes till the end of the string. Indexes are zero based. So for example, if we wanted to start at the beginning of the string, we'd use zero. If we wanted to skip the first character, we'd use one.
For the first half of the string, we don't want to skip any characters, so we'll use zero. The number of characters we want is half length of the string, so we pass in halfLength:
Dim firstHalf = txtString.Substring(0, halfLength)
For the second half, we want to start in the middle of the string, and consume characters till the end, so we'll use the other overload:
Dim secondHalf = txtString.Substring(halfLength)
You now have your string split in half.
The final result looks like this:
Dim txtString = "Hello World"
Dim halfLength = txtString.Length \ 2
Dim firstHalf = txtString.Substring(0, halfLength)
Dim secondHalf = txtString.Substring(halfLength)
Assuming the rules are "each side is half the length with the left side taking precedence", you would use Substring and some simple division:
Dim str As String = "william"
Dim part1 As String = str.Substring(0, CInt(Math.Ceiling(str.Length / 2.0#)))
Dim part2 As String = str.Substring(part1.Length)
part1 & " and " & part2 'will and iam
Here's a demo.
My code displays first half and last half of any number of characters entered.
Declare Variable
Dim strResults As String
Fetch text from textbox
strResults = Textbox1.Text
Display the first half of the text
MessageBox.Show(strResults.Substring(0, strResults.Length / 2), "First Half Characters")
Display the last half of the text
MessageBox.Show(strResults.Substring(strResults.Length / 2), "Last Half Characters")
Full code:
Dim strResults As String
strResults = Textbox1.Text
MessageBox.Show(strResults.Substring(0, strResults.Length / 2), "First Half Characters")
MessageBox.Show(strResults.Substring(strResults.Length / 2), "Last Half Characters")