Strings in Visual Basic Words, Phrases, and Spaces

Preview:

Citation preview

Strings in Visual Basic

Words, Phrases, and SpacesWords, Phrases, and Spaces

Strings are a series of characters.

Constant strings never change and Constant strings never change and are indicated by double quotes.are indicated by double quotes.Examples: Examples: “Fleeb” “Fleeb”

“Here is a “Here is a string.”string.”

Strings are a series of characters.

Variable strings are a special type Variable strings are a special type of variable.of variable.Example: Dim Fleeb as StringExample: Dim Fleeb as String

In any of the following operators In any of the following operators and functions you can use either a and functions you can use either a variable or constant string.variable or constant string.

Strings have their own operators and functions.

Operators for putting strings together.Operators for putting strings together.Functions for pulling strings apart.Functions for pulling strings apart.Functions for creating strings.Functions for creating strings.Functions for modifying strings.Functions for modifying strings.Operators for finding out things about Operators for finding out things about

strings.strings.

Putting Strings Together

& concatenates two strings& concatenates two stringsExamples:Examples:

““This “ & “is fun!” becomes “This is fun!”This “ & “is fun!” becomes “This is fun!”““Fleeb” & “norb” & “ski” becomes Fleeb” & “norb” & “ski” becomes

“Fleebnorbski”“Fleebnorbski”

Old Operator

The + sign can be used for the The + sign can be used for the same purpose, but it is better to use same purpose, but it is better to use the & since no one will mistake it the & since no one will mistake it for addition.for addition.

Functions for Pulling Strings Apart

Left$ - Pulls off the leftmost characters.Left$ - Pulls off the leftmost characters.Takes two arguments, Takes two arguments,

the string the string the number of characters to pull offthe number of characters to pull off

Examples: Examples: Left$(“Here is another string”,10) returns Left$(“Here is another string”,10) returns

“Here is an”“Here is an”Left$(“6 + 67 * 9”,6) returns “6 + 67” Left$(“6 + 67 * 9”,6) returns “6 + 67”

Pulling Strings Apart

Right$ - Pulls off the rightmost characters.Right$ - Pulls off the rightmost characters.Takes two arguments:Takes two arguments:

the stringthe stringthe number of characters to pull offthe number of characters to pull off

Examples: Examples: Right$(“Here is another string”,10) Right$(“Here is another string”,10)

returns “her string”returns “her string”Right$(“6 + 67 * 9”,6) returns “67 * 9”Right$(“6 + 67 * 9”,6) returns “67 * 9”

Pulling Strings Apart

Mid$ - Pulls out characters from middle.Mid$ - Pulls out characters from middle.Takes three argumentsTakes three arguments

the stringthe string the position to start pulling from.the position to start pulling from. the number of characters to pull.the number of characters to pull.

Examples:Examples:Mid$(“Here is another string”,10,4) returns “noth”Mid$(“Here is another string”,10,4) returns “noth”Mid$(“6 + 67 * 9”,5,2) returns “67”Mid$(“6 + 67 * 9”,5,2) returns “67”

Creating Strings

Str$ - Creates a string from a Str$ - Creates a string from a number.number.

CStr - Creates a string from a CStr - Creates a string from a number.number.

Format$ - Allows very complicated Format$ - Allows very complicated formatting of numbers.formatting of numbers.

Creating Strings

Format$ - Used with numbers, Format$ - Used with numbers, gives precise formatting control gives precise formatting control over strings.over strings.Takes two argumentsTakes two arguments

A numberA numberA descriptor.A descriptor.

Descriptors for Format$

Descriptors come in two flavors: system Descriptors come in two flavors: system defined and user defined.defined and user defined.

System defined include: General Number, System defined include: General Number, Currency, Fixed, Standard, Percent, Scientific, Currency, Fixed, Standard, Percent, Scientific, Yes/No, True/False, On/OffYes/No, True/False, On/Off

User defined are generated using another string. User defined are generated using another string. The other string has special characters in it that The other string has special characters in it that define how the number should be displayed. define how the number should be displayed.

User Defined Format String Characters

00 Display a number or zero.Display a number or zero.## Display a number or nothing.Display a number or nothing... Decimal placeholderDecimal placeholder%% Percentage placeholder.Percentage placeholder.,, Thousands seperatorThousands seperatorEE Scientific NotationScientific Notation:: Time seperatorTime seperator// Date seperatorDate seperator\ ”\ ” Literal character indicator.Literal character indicator.

User Defined Format String Characters

dd DayDay mm MonthMonth yy YearYear hh HourHour nn MinuteMinute ss SecondSecond ww Day of WeekDay of Week qq QuarterQuarter

Some Examples of Format

format(10201.2,”000000.00”) -> format(10201.2,”000000.00”) -> 010201.20010201.20

format(11.1,”####.##”) -> 11.1format(11.1,”####.##”) -> 11.1format(10010021,”###,###,###”) -> format(10010021,”###,###,###”) ->

10,010,02110,010,021format(1000000,”#.##E##”) -> format(1000000,”#.##E##”) ->

1.00E61.00E6

Some Examples of Format Using Time Formats

Dim curr_time as doubleDim curr_time as doublecurr_time = Nowcurr_time = Nowformat(cur_time,”mm/dd/yy hh:nn”) ->format(cur_time,”mm/dd/yy hh:nn”) ->

03/06/95 09:1503/06/95 09:15format(cur_time,”m/d/yy h:n”) -> format(cur_time,”m/d/yy h:n”) ->

3/6/95 9:153/6/95 9:15

More Examples

format(cur_time,”dddd, mmm dd, yyyy”) format(cur_time,”dddd, mmm dd, yyyy”) Wednesday, Dec. 06, 1995Wednesday, Dec. 06, 1995

Functions for Modifying Strings

UCase$ - Make every letter upper UCase$ - Make every letter upper case.case.

LCase$ - Make every letter lower LCase$ - Make every letter lower case.case.

Trim$ - Removes leading and Trim$ - Removes leading and trailing spaces from the string.trailing spaces from the string.

Modifying Strings

UCase$ - Make every letter upper UCase$ - Make every letter upper case.case.Takes one argument, the string.Takes one argument, the string.ExamplesExamples

UCase$(“Here”) returns “HERE”UCase$(“Here”) returns “HERE”UCase$(“Oh Boy”) returns “OH BOY”UCase$(“Oh Boy”) returns “OH BOY”

Modifying Strings

LCase$ - Makes every letter lower LCase$ - Makes every letter lower case.case.Takes one argument, the string.Takes one argument, the string.ExamplesExamples

LCase$(“Here”) returns “here”LCase$(“Here”) returns “here”LCase$(“Oh Boy”) returns “oh boy”LCase$(“Oh Boy”) returns “oh boy”

Modifying Strings

Trim$ - Removes leading and trailing Trim$ - Removes leading and trailing spaces from stringspaces from stringTakes one argument, the stringTakes one argument, the stringExamples Trim$(“ Here is a string “) Examples Trim$(“ Here is a string “)

returns “Here is a string”returns “Here is a string”Also comes in LTrim$ and RTrim$, for Also comes in LTrim$ and RTrim$, for

removing only one end or the other.removing only one end or the other.

Functions for Finding Things Out About Strings

Len - Finds out how long the string is.Len - Finds out how long the string is.InStr - Finding out if one string is in InStr - Finding out if one string is in

another.another.StrComp - Find out which string is StrComp - Find out which string is

“bigger”.“bigger”.

Finding Out about Strings

Len - Finds out how long the string is.Len - Finds out how long the string is.Takes one argumentTakes one argument

A stringA stringReturns the number of characters in Returns the number of characters in

the string.the string.

Finding Out about Strings

InStr - Finding out if one string is in another.InStr - Finding out if one string is in another.Takes two argumentsTakes two arguments

The string to look in.The string to look in.The string to look for.The string to look for.

Returns the location of the first occurance of Returns the location of the first occurance of string two in string one.string two in string one.

If the location equals zero, there is no If the location equals zero, there is no occurrence of string one in string two.occurrence of string one in string two.

Finding Out about Strings

StrComp - Which string is “bigger”.StrComp - Which string is “bigger”.Takes two arguments:Takes two arguments:

string onestring onestring twostring two

Returns:Returns:-1 if string one < string two-1 if string one < string two0 if they are equal0 if they are equal1 if string two < string one1 if string two < string one

Summary

String operations can be very String operations can be very useful.useful.

VB gives particularly strong VB gives particularly strong support for strings.support for strings.