But how long is a piece of string?!
We need to use some form of String Manipulation, when we want to:
concatenate (link together) two text strings (&)
concatenate a text string and a variable (&) (maybe in a messagebox)
store data in files (LSet)
pick out certain characters from a textbox (Mid)
match characters in a textbox with text in a program (Trim)
retrieve data from files (Trim/Mid)
search for a particular customer in a file (Trim/Mid)
compare strings
Click on these links to go the the section you need------>>>>>
Let's start with the simplest form of string manipulation - concatenation
Concatenate 2 strings:
A big word, it simply means adding two strings of characters together, so they are output as one.
This example shows a surname and firstname being strung together ---->
Note: the " " in the middle puts a space in between the two strings, to make it more readable.
Note2: the & character is needed to tell the computer we are stringing something together.
So we are stringing together a firstname, followed by a space, followed by a surname. In this example, our output is 'James Smith'/
Concatenate strings and variables:
When the Input button is clicked, this example shows a string and a variable being concatenated in a MessageBox.
Code:
As you can see, you can display different buttons in your message box. Here are some others. You can use their numbers (instead of the '1' above), or you can use the 'vbOKCancel' instead of the number. Try a few of these out to see how the message box buttons change.
Lastly, you could then check which button has been pressed to run some code :
When you store data in text files, you need the field lengths to be the same in each record saved. Each field can have a different length (username, password, dob), but each username (dor example) must be 10 characters.
If the username is less than 10 characters, we store the characters of the username and pad out with spaces to 10 characters
e.g. Smith is 5 characters, so we then need to save 5 spaces on the end
e.g. Carpanini is 9 characters, so we then need to save 1 space on the end.
The LSet function allows us to do this. We give it the text string and tell it the maximum number of characters the username can be saved as in the textfile.
In the example code below, we are saving to a file (look at the WriteLine command) with the following file structure:
UserId 10 chars
FirstName 10 chars
Surname 10 chars
Username 10 chars
Note: Each field could be a differing number of characters, if it is appropriate
Records written to names.txt:
Everything stored in a textfile is classed as a string of characters, regardless of whether they are letters, numbers, spaces or punctuation.
We can extract any number of characters from any point in a string (of characters), using the Mid function.
There are 3 parts to the Mid function (inside the brackets)
The string you want to get characters from (txtString.text in this case)
The first number = the position in the string you want to start at
The second number = how many characters you want to find
Note: in the second example, we want the next 5 characters. The first 5 characters are in positions 1 to 5, so the next 5 characters start in position 6. Where would the third set of 5 characters start from?
This string has 23 characters in total (count the spaces and the punctuation).
This message box displays the first 5 characters of the string in the textbox
This message box displays the next 5 characters of the string in the textbox
You can use the Trim function to get rid of spaces at the end of a string of characters - useful for when creating a login system, as users sometimes put a space at the end of their username.
If you didn't use the Trim function, the login would be unsuccessful, even though you typed the username correctly.
"17JonesE2 " is not the same as "17JonesE2", so the login would fail.
Note: We use the = sign to compare the two strings Trim(txtUserName.text) and "17JonesE2"
To retrieve particular records from a textfile, we make use of the Trim AND Mid functions together. We need to match on certain criteria, and then pull out the correct fields (characters) from the record(s) we have found.
You can find coding examples of this type below. However on the File Handling page of this website, in Sections 2 (Searching for a record in a text file) & 3 (Creating a Login system), you will find explanations of how the code works.
When using the above code, each record in the textfile is checked.
If the username entered = first 10 characters of record
AND
password entered = next 10 characters of record,
then user is allowed to log in.