Override String in TextBox - vb.net

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.

Related

How to add spaces for certain condition using vb.net?

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...

How to Remove Specific Text from Combobox Items

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.

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();

String range alternative in vb.net

Is there any function similar to string range in vb.net 2.0 ??
What I am try to achieve here is to extract some text from a string with unknown length.
eg.
given string = text text text mytext1 text text text text mytext2 text text text text
expected string = mytext1 text text text text mytext2
So I have the indexes for "mytext1" and "mytext2". I am looking for a way to get the text that wrapped in between those two strings or indexes.
Thanks
Well, what’s wrong with String.Substring? It works on indices so if you want to find text delimited by two words, you first need to find their respective indices using String.IndexOf.
Dim from = given.IndexOf("mytext1")
Dim [to] = given.IndexOf("mytext2")
Dim result = given.Substring(from, [to] - from + "mytext2".Length)
(Note that To is a reserved word so I need to put the identifier in square braces … or use another identifier. ;-))
Assuming that your End Index is at the end of "myText2" then you can do this...
Dim strExpectedString = Mid(strGivenString, intStartIndex, (intEndIndex - intStartIndex))
Otherwise just add the length of "myText2" to intEndIndex.

How to find the space using substring to display initials

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.