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();
Related
Let say, maximum length in textbox1 is 6 digits. so if the user enter less than 6, i want to add spaces in front of the text. I have no idea how to do that.
Example:
TextBox1 = "123"
output = " 123"
System.String has a method called PadLeft - it adds whatever char you want to the left of the string to make it whatever length you choose:
Dim str As String
Dim pad As Char
str = "123"
pad = "."c ' Using dots instead of spaces so you can see it...
Console.WriteLine(str)
Console.WriteLine(str.PadLeft(6, pad))
Result:
123
...123
You can see a live demo on rextester.
BTW, it also has PadRight...
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.
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.
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.
The part of the program I'm trying to write is supposed to take the users input, which is their name, and in a listbox spit out their initials. I have so far:
Dim initials As String
Dim indexspace As Integer
initials = name.Substring(0, 1)
indexspace = name.IndexOf(" ")
lstInvoice.Items.Add(String.Format(strFormat, "Invoice ID", initials & space))
When I run the program, I can get the first initial to pop up, but I am not certain how to get the second to pop up.
Thank you very much for your assistance on this matter, it is much appreciated
Q: How do I find a space (" ") in a string with VB.Net?
A: String.IndexOf (" ");
Q: How do I find subsequent spaces in the string?
A: Extract the substring of everything to the right of the first space you found.
Then do an ".IndexOf (" ")" of that substring.
Until you get to the end of the string, or until you get bored :)
I apologize for not writing it in VB but I am more familiar with C#. There is no reason to look for the index of a space. Just split the string on the space and then pull the first letter of each of the results in the array. For simplicity, I made an assumption that it was only two words in the string but you could loop through the array of results to put out all the initials if they included a middle name:
string name = "Test User";
string[] nameParts = name.Split(' ');
string initials = nameParts[0].Substring(0, 1) + nameParts[1].Substring(0, 1);
VB Version:
Dim name As String = "Test User"
Dim nameParts As String() = name.Split(" ")
Dim initials As String = nameParts(0).Substring(0, 1) & nameParts(1).Substring(0, 1)
Again this is a very simplistic approach to what you are asking. As #paxdiablo pointed out in his comments there are variations of names that will not match this pattern but you know you're program.