String Manipulation

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
3 posts Page 1 of 1
Contributors
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

String Manipulation
GoodGuy17
String Manipulation Tutorial

Introduction:
Hello, everyone! In this tutorial, we will be learning about the uses of strings. We will learn how to edit, change, find, and many more things involving coding with strings. Remember, if this tutorial has been helpful, please +rep me. Here we go!

Tutorial:
The first thing we are going to learn about is the Trim function. The Trim function removes all leading and trailing spaces in a string. This is useful in things like email apps, and more. Example: If I type " hello@yahoo.com " in a textbox, then in a button type the code for the Trim function, it will make the text "hello@yahoo.com" instead.

Code:
Code: Select all
TextBox1.Text = TextBox1.Text.Trim
The above code will simply trim the leading and trailing spaces in TextBox1's text.

----------------------------

The next thing we will learn about is the InStr function. What this does is find the index of the first occurence of a string specified. Example: If I type "hello@yahoo.com" in a textbox, then in a button type the code for the InStr function to find the index of the character "@", it will return a value of 6, because the "@" character of the string.

Code:
Code: Select all
Dim strEmail As String = "hello@yahoo.com"
Dim intIndex As Integer

intIndex = InStr(strEmail, "@")

MsgBox(intIndex)
The above code will declare 2 variables, strEmail and intIndex, which hold the email and index of the letter we are searching for, respectively. Then, it will set intIndex's value to the index of the first occurence of the "@" character in our email. Finally, a message box will pop up showing us the value stored in intIndex.

----------------------------

Another thing we will learn about is the Substring function. This function will return a string within a string, starting and grabbing a specified amount of characters, at a specified index. Example: If I type "hello@yahoo.com" in a textbox, then in a button type the code for the Substring function to find the "yahoo" text in our email, we would have to start at 6, then grab 5 characters.

Code:
Code: Select all
Dim strEmail As String = "hello@yahoo.com"
Dim strYahoo As String

strYahoo = strEmail.Substring(6, 5)

MsgBox(strYahoo)
What that code does is declares 2 variables to hold our strings. Then, it grabs 5 characters starting at index 6 in strEmail, and sets strYahoo to the string found. Finally, a message box shows us the string in strYahoo.

----------------------------

Yet another thing we can learn of is the Split and Join functions. This is a very useful piece of code to use.

Split description: The Split function returns multiple values it found between a specified seperator. Example: If I type "username, password, bobby, hill" in a textbox, then in a button type the code for the Split function, it will return "username", "password", "bobby", and "hill".

Code:
Code: Select all
Dim array(3) As String

array = TextBox1.Text.Split(",")

For i = 0 to UBound(array)
MsgBox(array(i))
Next
The code above creates an array with 4 indexes, sets the text in each slot to the text found by splitting the text from the ",", and goes through a loop through each index and shows a message box containing each value in each index.

Join description: The Join function joins multiple values together with a specified seperator. Example: If I type "username, password, bobby, hill" in a textbox, then in a button type the code for the Join function, it will return "username,password,bobby,hill".

Code:
Code: Select all
Dim array(3) As String
array(0) = "username"
array(1) = "password"
array(2) = "bobby"
array(3) = "hill"

MsgBox(String.Join(",", array))
What that code does is creates an array with 4 indexes, sets the text in each slot to the text specified, then shows a message box with the array's values joined together with a comma.

----------------------------

Examples:
Here is an example I just whipped up by request from Axel (sorry I took so long, never read your post until now). What this code will do is search through a textbox for a string specified by you and select it. So, if I have 5 items in a listbox, "Joe, Tom, Bob, Bill, Hank", and I search for "Bill", it will select "Bill". Add a button and a listbox to your form. Put some items in the listbox.

Code:
Code: Select all
Dim strSearch As String
strSearch = InputBox("Enter the string you wish to search for.")
For i = 0 To ListBox1.Items.Count - 1
ListBox1.SelectedItem = ListBox1.Items.Item(i)
If ListBox1.SelectedItem = strSearch Then
MsgBox("String found and selected.")
Exit Sub
End If
Next
This will make a string which contains the search query, then it will loop the items in the listbox, select each, and check if the string selected is the one we stored in strSearch.

----------------------------

Outro:
Thanks for reading, guys! Remember to +rep if this helped! Please reply telling me your opinions, and please only give constructive criticism if you must tell me of a problem. Be sure to watch out for some more tutorials, I will be spurting out some more later this month!

~GoodGuy17 :D
Last edited by GoodGuy17 on Fri Mar 18, 2011 1:10 am, edited 1 time in total.
User avatar
XTechVB
VIP - Site Partner
VIP - Site Partner
Posts: 727
Joined: Thu May 20, 2010 10:32 am

Re: String Manipulation
XTechVB
Nice tutorial Dude, very helpful for beginners, Strings are very easy to understand and very importantm for they're used in almost every application,

Nice Tutorial, Keep it Up
You can find me on Facebook or on Skype mihai_92b
User avatar
Axel
Coding God
Coding God
Posts: 1928
Joined: Sun Jun 27, 2010 9:15 pm

Re: String Manipulation
Axel
You should add basic algorithms to it like , search for a string in a listbox and sort alphabetically (NOT by IO or something)
http://vagex.com/?ref=25000
3 posts Page 1 of 1
Return to “Tutorials”