Beginners Guide to Strings in VB.net
Another Nextgen1 Article/Tutorial
Note: as for all my tuts and articles , The best way to learn is to experience what you are trying to learn, So open up Vb.net and follow along, don't just copy and paste what is needed, your never going to learn that way.
1. Length of a string (Variable)
Add to your Form a Textbox, a Label and A button (don't worry to much about the properties of these, or how it looks on the form, this is not a guide to UI)
To get the length of a string , we use .Length
Here is an example code, add this to the Button Click Event
Code:
Label1.text = Textbox1.Text.Length
Start the program, add some text to the TextBox and click the button. Your label should give you the length of the string or variable entered.
2. Search using IndexOf
Something everyone is looking to do every now and then is search text for a particular word programatically, In order to do this we will have to use the IndexOf (Find word, StartPosition) function.
The IndexOf command returns its value as an integer at the place where it finds the string in the text.
This code starts at the beginning of 'I like programming and writing tutorials', because we didn't give a start position, and searches for the word 'writing' in it.
If it does not find the word "writing" in the string, then it will return the value as 0, and your label will say '0'. in which case you can use
Code:
If label1.text = 0 then msgbox ("Could not find the word you are looking for")
' You can be more advanced, but for simplicity reasons, im going to keep this ' short
However, if it finds the word, then it returns a number saying where it found the start of the word. In this case, your label should say '25'(I think I counted right) because the 'w' of the word writing is 25 characters into the string.
Code:
Dim S As String = "I like programming and writing tutorials"
S = S.IndexOf("writing")
Label1.text = S
3. Replace Text or characters with another text or character.
Next on our list of things to learn is .Replace which is used to find and replace text in a string.
The Replace function returns the text that it has replaced.
Code:
Dim R As String = "I like programming"
R = R.Replace("Like", "Love")
LabelBox1.text = R
4. UPPERCASE to lowercase
This is useful for making sure that if a user types something in uppercase then it will still comply with something in your code that is lowercase. (very handy for a lot of applications)
To make a sentence uppercase, you use the following:
Code:
Dim U as String
U = "I love PROGRAMMING"
U = *****Upper
Label1.text = U
Label1 should now contain the words 'I LOVE PROGRAMMING'
To convert to lowercase, use the following:
Dim L as String
L = "I love PROGRAMMING"
L = L.ToLower
Label1.text = L
Label1.text should now contain the words 'i love programming'
5. Reversing the order.
At first I was not going to add this part to the tutorial because lack of use, however, I figured there may be someone who could use it, So I am sharing StrReverse
If you wish to flip around a string, then the StrReverse(string) will do it
Code:
Label1.text=(StrReverse("I love programming"))
You should get the result "gnimmargorp evol I"
Well that basically covers a beginners Guide, If you request I will do a advanced guide containing 5 more string manipulation concepts and uses.
Hope this helps those he need it