270 likes | 415 Views
BACS 287. Visual Basic String Manipulation. Visual Basic Strings. In Visual Basic, a “string” is a series of text, numbers, and special characters. Strings are stored in string variables. String values are always enclosed in quotation marks.
E N D
BACS 287 Visual Basic String Manipulation BACS 287
Visual Basic Strings • In Visual Basic, a “string” is a series of text, numbers, and special characters. • Strings are stored in string variables. • String values are always enclosed in quotation marks. • There is only 1 built-in operator that works on strings (the ‘&’ operator). BACS 287
String Characteristics • All strings in Visual Basic are variable-length strings. • A variable-length string can contain up to approximately 2 billion characters. • Generally speaking, string variables can contain any character represented on the keyboard. BACS 287
String Examples • The following are examples of defining and manipulating strings. Dim strInput as string = “ABC 123” Dim strOutput as string = “def ,/ 56r” Dim strX as string strX = strInput & strOutput Results in strX value of “ABC 123def ,/ 56r” BACS 287
Operations on Strings • There are 2 basic ways to manipulate strings in Visual Basic • Using built-in string functions • Using string methods • Methods associated with string variables are the more modern way to manipulate strings • You can use either methods or functions; both achieve the same results; however, methods are the preferred way. BACS 287
String Methods • There are several methods automatically associated with all string variables. Determine current string state: string.Length – returns the number of characters in a string string.Compare – determine if two strings are the same string.IsNullOrEmpty – determine if a string is null or empty string.StartsWIth – determine if a group of characters is at start of a string string.EndsWith – determine if a group of characters is at end of a string string.Contains – determine if a string contains a group of characters (Boolean) string.IndexOf – find the starting location of a substring within the string BACS 287
String Methods Modify edges of string: string.TrimStart – removes leading characters from a string string.TrimEnd – removes trailing characters from a string string.Trim – removes leading and trailing characters from a string string.PadLeft – adds characters to the beginning of a string string.PadRight – adds characters to the end of a string Convert string to different format: string.ToUpper – convert all characters to upper case string.ToLower – convert all characters to lower case BACS 287
String Methods Add / Remove characters in existing string: string.Remove – removes characters from anywhere in a string string.Substring – extract a substring from the string string.Replace – replaces characters with other characters in a string string.Insert – adds characters within a string Manipulate string into different string / array: string.CopyTo – copies a specified number of characters to an array string.Split – returns a string array of delimited elements from the original string BACS 287
String Functions • Most of the same string manipulations can be performed with system defined functions. INSTR - returns the position of the first occurrence of one string within another MID - Returns the specified number of characters from a string LEN - Returns the number of characters in a string LEFT / RIGHT - Returns the specified number of characters from the left/right side of a string. UCASE / LCASE - Returns a string that has been converted to uppercase or lowercase BACS 287
String Functions TRIM / RTRIM / LTRIM - Returns a copy of a string without leading spaces (Ltrim), trailing spaces (Rtrim), or both leading and trailing spaces (Trim). FORMAT - Formats an expression according to instructions contained in a format expression. BACS 287
InStr Function • The InStr function is used when you want to find the first occurrence of one string within another. • It returns the position of where the match is found, 0 if it is not found. Syntax: InStr (start, string1,string2,[compare]) BACS 287
InStr Function Example SearchString ="XXpXXpXXPX XP" ' String to search in (note that position 11 is a' space). SearchChar = "P" ' Search for "P". IntStartPos = 7 ' A textual comparison starting at position 4. Returns 6. MyPos = InStr(4, SearchString, SearchChar, 1) ' A binary comparison starting at position 1. Returns 9. MyPos = InStr(1, SearchString, SearchChar, 0) ' Comparison is binary by default (last argument is omitted). MyPos = InStr(SearchString, SearchChar) ' Returns 9. MyPos = InStr(intStartPos, SearchString, " ") ' Returns 11. MyPos = InStr(intStartPos, SearchString, "p") ' Returns 0. BACS 287
Mid Function • The MID function is used to extract characters from a string. • It is commonly used with InStr to pull out the sub-string that was located. Syntax: Mid (string,start,[length]) BACS 287
Mid Function Example MyString = "Mid Function Demo" ' Assign text string. FirstWord = Mid(MyString, 1, 3) ' Returns "Mid". LastWord = Mid(MyString, 14, 4) ' Returns "Demo". MidWords = Mid(MyString, 5) ' Returns "Function ' Demo". strOut = Mid(MyString, InStr(1,MyString,”F”), 8) ' Returns “Function” InStr function returns 5 BACS 287
Determine String Length • The Length method and the Len function may be used to determine the number of characters in a string. • The Len function can also be used to determine the number of bytes to store a variable (somewhat obscure). Syntax: string.length Len (string | variable) BACS 287
Determine String Length Example Dim MyString as string = “Hello World” Dim MyInt As Integer Dim MyCur As Currency ‘ use method MyLen = MyString.Length ‘ returns 11 ‘ use function MyLen = Len(MyString) ' Returns 11. MyLen = Len(MyInt) ' Returns 2. MyLen = Len(MyCur) ' Returns 8. BACS 287
Modify String Edges Operations • The Left and Right functions are used to extract characters from the left and right side of a string. • The original string is not modified Syntax: Left (string,length) Right (string,length) BACS 287
Left / Right Example AnyString = "Hello World" ' Define string. MyStr = Left(AnyString, 1) ' Returns "H". MyStr = Left(AnyString, 7) ' Returns "Hello W". MyStr = Left(AnyString, 20) ' Returns "Hello World". MyStr = Right(AnyString, 1) ' Returns "d". MyStr = Right(AnyString, 6) ' Returns " World". MyStr = Right(AnyString, 20) ' Returns "Hello World". BACS 287
Ucase / Lcase Function • The Ucase and Lcase functions return a string with the case changed to upper or lower. Syntax: Ucase (string) Lcase (string) BACS 287
Ucase / Lcase Example strInput = "Hello World 1234" ' String to convert. UpperCase = UCase(strInput) ' Returns "HELLO WORLD 1234". LowerCase = LCase(strInput) ' Returns ”hello world 1234". BACS 287
Ltrim / Rtrim / Trim Function • The Ltrim, Rtrim, and Trim functions remove leading spaces, trailing spaces, and both leading and trailing spaces. Syntax: LTrim (string) RTrim (string) Trim (string) BACS 287
LTrim / Rtrim / Trim Function MyString = " <-Trim-> " ' Initialize string TrimString = LTrim(MyString) ' TrimString = "<-Trim-> ". TrimString = RTrim(MyString) ' TrimString = " <-Trim->". TrimString = LTrim(RTrim(MyString)) ' TrimString = "<-Trim->". ' Using the Trim function alone achieves the same result as the previous example. TrimString = Trim(MyString) ' TrimString = "<-Trim->". BACS 287
Format Function • The Format function is used format a string expression based upon instructions given in a “format expression”. • The rules for this function are very complex and are best learned via the VB help screens. Syntax: Format(expression,[format,[firstdayof week,[firstweekofyear]]]) BACS 287
Format Function Examples MyTime = #17:04:23# MyDate = #January 27, 1999# ' Returns current system time in the system-defined long time format. MyStr = Format(Time, "Long Time") ' Returns current system date in the system-defined long date format. MyStr = Format(Date, "Long Date") MyStr = Format(MyTime, "h:m:s") ' Returns "17:4:23". MyStr = Format(MyTime, "hh:mm:ss AMPM") ' Returns "05:04:23 PM". MyStr = Format(MyDate, "dddd, mmm d yyyy") ' Returns "Wednesday, ' Jan 27 1993". BACS 287
Format Function Examples ' If format is not supplied, a string is returned. MyStr = Format(23) ' Returns "23". ' User-defined formats. MyStr = Format(5459.4, "##,##0.00") ' Returns "5,459.40". MyStr = Format(334.9, "###0.00") ' Returns "334.90". MyStr = Format(5, "0.00%") ' Returns "500.00%". MyStr = Format("HELLO", "<") ' Returns "hello". MyStr = Format("This is it", ">") ' Returns "THIS IS IT". See Visual Basic help screens for more format expression details. BACS 287
Nesting String Functions • You can nest string functions inside each other. The innermost function is performed first and the outermost is performed last. Example: Dim MyString as string = ” nested function demo “ strX =Mid(Ucase(Trim(MyString)), InStr(1,Ucase(Trim(MyString)),”UNC”), 8) ' Returns “UNCTION ” BACS 287
Nesting String Functions • This also works with string methods. Example: Dim MyString as string =” nested function demo “ strX = MyString.Substring((MyString.Trim.ToUpper.IndexOf("UNC", 0) + 2), 8).ToUpper ' Returns “UNCTION ” BACS 287