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
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:
----------------------------
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:
----------------------------
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:
----------------------------
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:
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:
----------------------------
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:
----------------------------
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
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
The above code will simply trim the leading and trailing spaces in TextBox1's text.TextBox1.Text = TextBox1.Text.Trim
----------------------------
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
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.Dim strEmail As String = "hello@yahoo.com"
Dim intIndex As Integer
intIndex = InStr(strEmail, "@")
MsgBox(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
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.Dim strEmail As String = "hello@yahoo.com"
Dim strYahoo As String
strYahoo = strEmail.Substring(6, 5)
MsgBox(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
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.Dim array(3) As String
array = TextBox1.Text.Split(",")
For i = 0 to UBound(array)
MsgBox(array(i))
Next
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
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.Dim array(3) As String
array(0) = "username"
array(1) = "password"
array(2) = "bobby"
array(3) = "hill"
MsgBox(String.Join(",", array))
----------------------------
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
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.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
----------------------------
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

Last edited by GoodGuy17 on Fri Mar 18, 2011 1:10 am, edited 1 time in total.
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
Nice Tutorial, Keep it Up
You can find me on Facebook or on Skype mihai_92b
You should add basic algorithms to it like , search for a string in a listbox and sort alphabetically (NOT by IO or something)
3 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023