How to Remove Specific Text from Combobox Items - vb.net

What I want to do is be able to remove a section of a string and use the remaining portion of it to add to a Combobox.
Dim randString As String = ""
textbox.text = "(Remove this) - Keep this"
randString = textbox.text.... ' Trim/Split the first portion off somehow?
combobox.items.add(randString)
' Result should look like " - Keep this"
I've tried Trim/Split without any luck. Could anyone point me in the right direction? Thanks.

If you just want to remove a portion of the string, you can use the Replace function:
randString = Replace(textbox.text, "(Remove this)", "")
You can put any string variable in as the second argument.

Related

Override String in TextBox

I have 3 text boxes on my form: Surname, First Name and Middle Name. I want to override my string. It looks like this:
...<<<<<<<<<<<<<<<<... (Length = 36)
If I save a value and the text boxes contain Surname:Bergs, First Name:John Paul, Middle Name:Dale, and for sample purpose I want to display this in MessageBox, and should be like this
D<aleBergs<<John<Paul<<<<<<<<
I have 2 objectives in this Question.
How can I get the Text box specific value and display it on my string?
If Text Box Contains space how can I turn it into < value?
Update
I solved Second Problem fix using this String.Replace()
Dim str As String = "John Paul"
Dim str2 As String = str.Replace(" ", "<")
MessageBox.Show(str2)
Use pre-fabricated string to have clear format
Dim outputFormat As String = "{0} {1} {2}"
MessageBox.Show(string.Format(outputFormat, txtMid.Text, txtFirst.Text, txtLast.Text))
You can also use format like {0, 10} or {0,-10} to get fixed positions, like this
|John......|
|......John|
Where dots stand for spaces
If i'm right, You can get textbox inputs to separate string variables and concatenate them into one string variable or you can get all textbox values to one string variables like
Dim AllStrings As String
AllStrings = MiddleTextBox.Text &" "& FirstTextBox.Text &" "& LastTextBox.Text
Then you can replace spaces by using Replace Method.
AllStings = AllStrings.Replace(" ","<")
Hope this will helpful to you.

How to get the first value in textbox

how can i get the first value in a text box and place it to a label. Example, I want to get the word "look" in the textbox that I input was "look like".
You could use substring, this will split the string or aka your textbox.
This takes two params, The starting index of the string and the 2nd param is the length. I have put str.indexof in the 2nd param to get the index of where the space is.
dim str as string
Label1.Text = str.Substring(0, str.IndexOf(" "))
This code is not tested, I only used visual basic for a little bit when i was 12.
you can simply split the value by " " space and take the first one from result array
if(!String.IsNullOrEmpty(textBox1.Text))
{
lable1.Text = textBox1.Text.Split(" ").first();
}
textboxValue = Textbox.Text;
str = textboxValue.Split(" ").first();

Lowercase the first word

Does anybody know how to lowercase the first word for each line in a textbox?
Not the first letter, the first word.
I tried like this but it doesn't work:
For Each iz As String In txtCode.Text.Substring(0, txtCode.Text.IndexOf(" "))
iz = LCase(iz)
Next
When you call Substring, it is making a copy of that portion of the string and returning it as a new string object. So, even if you were successfully changing the value of that returned sub-string, it still would not change the original string in the Text property.
However, strings in .NET are immutable reference-types, so when you set iz = ... all you are doing is re-assigning the iz variable to point to yet another new string object. When you set iz, you aren't even touching the value of that copied sub-string to which it previously pointed.
In order to change the value of the text box, you must actually assign a new string value to its Text property, like this:
txtCode.Text = "the new value"
Since that is the case, I would recommend building a new string, using a StringBuilder object, and then, once the modified string is complete, then set the text box's Text property to that new string, for instance:
Dim builder As New StringBuilder()
For Each line As String In txtCode.Text.Split({Environment.NewLine}, StringSplitOptions.None)
' Fix case and append line to builder
Next
txtCode.Text = builder.ToString()
The solutions here are interesting but they are ignoring a fundamental tool of .NET: regular expressions. The solution can be written in one expression:
Dim result = Regex.Replace(txtCode.Text, "^\w+",
Function (match) match.Value.ToLower(), RegexOptions.Multiline)
(This requires the import System.Text.RegularExpressions.)
This solution is likely more efficient than all the other solutions here (It’s definitely more efficient than most), and it’s less code, thus less chance of a bug and easier to understand and to maintain.
The problem with your code is that you are running the loop only on each character of the first word in the whole TextBox text.
This code is looping over each line and takes the first word:
For Each line As String In txtCode.Text.Split(Environment.NewLine)
line = line.Trim().ToLower()
If line.IndexOf(" ") > 0 Then
line = line.Substring(0, line.IndexOf(" ")).Trim()
End If
// do something with 'line' here
Next
Loop through each of the lines of the textbox, splitting all of the words in the line, making sure to .ToLower() the first word:
Dim strResults As String = String.Empty
For Each strLine As String In IO.File.ReadAllText("C:\Test\StackFlow.txt").Split(ControlChars.NewLine)
Dim lstWords As List(Of String) = strLine.Split(" ").ToList()
If Not lstWords Is Nothing Then
strResults += lstWords(0).ToLower()
If lstWords.Count > 1 Then
For intCursor As Integer = 1 To (lstWords.Count - 1)
strResults += " " & lstWords(intCursor)
Next
End If
End If
Next
I used your ideas guys and i made it up to it like this:
For Each line As String In txtCode.Text.Split(Environment.NewLine)
Dim abc() As String = line.Split(" ")
txtCode.Text = txtCode.Text.Replace(abc(0), LCase(abc(0)))
Next
It works like this. Thank you all.

VB.NET Get text in between Quotations or other symbols

I want to be able to extract a string in between quotation marks or parenthesis etc. to a variable. For example my text might be "Hello there "Bob" ". I want to extract the text "Bob" from in between the two quotation marks and put it in the string "name" for later use. The same would be for "Hello there (Bob)". How would I go about this? Thanks.
=======EDIT======
Sorry, I worded this poorly. Ok, so lets say I have a textbox(Textbox1) and a button. If the user inputs the text: MsgBox "THIS IS MY MESSAGE" I want that when the Button is pressed, only the text THIS IS MY MESSAGE is displayed.
This is a solution very simple:
Dim sAux() As String = TextBox1.Text.Split(""""c)
Dim sResult As String = ""
If sAux.Length = 3 Then
sResult = sAux(1)
Else
' Error or something (number of quotes <> 2)
End If
There are basically three methods -- regular expressions, string.indexof and substring and finally looping over the characters one by one. I would avoid the latter as it is just reinventing the wheel. Whether to use regexs or indexof depends upon the complexity of your requirements and data. Indexof is a bit wordy but fairly straightforward and possibly just what you want in this case.
Dim str as String = "Hello there ""Bob"""
Dim startName as Integer
Dim endName as Integer
Dim name as String = ""
startName = str.IndexOf("""")
endName = str.Indexof("""", If(startName > 0, startName,0))
If (endName>startName) Then
name = str.SubString(startName, endName)
End If
If you need to do this for arbitrary symbols, then you want regexs.

Convert URI to String

Visual Basic 2010.
Dim selection As String = ListBox1.SelectedItem
Dim url As String = Split(selection, " - ")
Form1.WebBrowser1.Navigate(url(1))
I want to convert the URI (Value of type '1-dimensional array of string' cannot be converted to 'string') to a string. How would I do so?
(The selection variable is something like "Title - URL")
Thanks!
The error is on Split(selection, " - ")
May be your error is at Getting the Split Parts of the Selection to the Url, where you have used normal string instead of One-Dimensional Array Declaration.
Split function will return array of string. But you are trying to assign its value to "string". To declare a array use the below code
Dim url() As String = Split(selection, " - ")
To know about vb array refer the link.
http://www.startvbdotnet.com/language/arrays.aspx
Also one wise way to store url in ListItem is, Set the URL in Value field and Display text in Text field. So that, you can retrieve url from value easily without any string processing.