intStart is an optional position parameter within strMyString at.strRplWith is the string to be used to replace strFind.strFind is the string to find in strMyString.strMyString is the string to be changed.Parameter intStart with a value greater than 1, the return string won't contain the String to which the function was directed to work on. StrResult = Replace( strMyString, strFind, strRplWith, _Ī feature of the Replace() function is that it returns only the portion of the The syntax for the Replace() function is as follows: Processing program, you have a good understanding of how this function works. If you've ever used the replace feature in a word Find operations also can eitherĬonsider or ignore capitalization. Or remove the found string within the target string. The next new function to introduce is Replace(), which can find and then replace ![]() Notice, however, that because a simple assignment statementĬould be used, the value of txtResult.Text didn't have to be initialized, further Listing 12.2's seven code lines are more for contrast than function. 03 04 txtResult.Text = StrReverse(txtMyString.Text) 05 Refresh 06 07 End Sub LISTING 12.2 12LIST02.TXT-Using the New StrReverse() Function 01 Private Sub NewVersion_Click() 02 `This new function works on strings. Search to go to the Book Info page for Using Visual Basic 6. At the prompt, enter 078971633x (for the book's ISBN) and then click You can find all the code for the examples in this chapter on the Web at. 12 For intCntr = 1 To intLength 13 txtResult.Text = Mid(txtMyString.Text, intCntr, 1) _ & txtResult.Text 14 Next intCntr 15 Refresh 16 17 End Sub 11 `Concatenate each character to the beginning. The Len() Function 01 Private Sub OldVersion_Click() 02 Dim intLength As Integer `Length of the string 03 Dim intCntr As Integer `Loop counter 04 05 `Initialize the txtResult.Text string and then 06 ` determine the length of txtMyString.Text 07 intLength = Len(txtMyString.Text) 08 txtResult.Text = "" 09 10 `Loop through the first string one character at a time. LISTING 12.1 12LIST01.TXT-A VB5 For.Loop That Reverses a String Is reversed by either the old or the new string-reversing subroutines. The code in Listing 12.2 delivers the same result with the new StrReverse()įunction, with no loops and only one simple command. To step through each character in the string and then build the new string in reverse Listingġ2.1 shows the code required for Visual Basic 5. The sample program in Figure 12.1 uses two ways of reversing a string. Perform a string function on the null data field. The problem happens when you takeĪ null data field from a database and assign it to an internal variable or when you Visual Basic defaults all internal variables to valid values-numeric variablesĭefault to zero, and strings default to empty. These null values will cause problems for the string functions.Ġ2 txtMyString.Text = "It's raining cats and dogs." Most databases, such as MicrosoftĪccess and SQL Server, allow fields to be null or undefined until they're initialized. Important to distinguish this from a null string. Notice that line 3 of the following code is an empty string of length zero. ![]() ![]() The TextBoxes shown in Figure 12.1 were initialized during the form's load event. strMyString is the string to be reversed.strResult is the string returned from the function.Visual Basic's new StrReverse() function enables you to completely reverse the Reversing the Order of Characters in Strings New string functions, expect to refer to this chapter several times before you uncoverĪll that can be accomplished with these new functions. Seasoned VB programmers can now replace entireĬustom functions in their programs with a single line of code. ![]() Visual Basic 6 comes with a lot of new string functions, many of them vast functionalĮnhancements to the native language. The first task when working with strings is to be able to locate parts of a string Changing Data Types with the Conversion Functions.Controlling Data Types with Typecasting.Changing a String's Case with UCase() and LCase().Combining an Array of Strings with Join().Creating a Sublist from an Array of Strings with Filter().Splitting a String into an Array of Strings with Split().Using InStr() to Search for Characters Within a String.Using Mid() to Return Characters from Inside a String.Truncating Strings with Left() and Right().Determining a String's Length with Len().Reversing the Order of Characters in Strings.Using Visual Basic 6 - Ch 12 - Working with Strings and Typecasting
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |