VB Strings

VB Strings

String Length

                         

Comparing Strings

Strings in VB.NET are compared using the String Compare method.  The return value can be -1 (if the first string is less than the second string), zero (if the two strings are identical), or 1 (if the second string is less than the first).

 

The CompareTo method is an instance method of String class and works similarly to the Compare method.

 

Finding a substring

The IndexOf method returns the position of a given substring within a string.  If the substring is not found, the position returned is -1.  This method is case sensitive.

 

Extracting a substring

The Substring method pulls out a portion of a string starting at a given position.  The length of the substring is optional and if it is omitted, then the rest of the string is returned.

 

Copy and Concatenating Strings

 

Adding, Removing and Replacing Strings

The Insert method allows a string to be inserted inside another one at a defined position.  The first position is zero.

 

The Remove method takes away characters starting at a defined position.

 

 The Replace method substitutes one string for another.

 

The Split method creates an array of strings given a defined separator string, here it is ",".

 

Uppercase and Lowercase

The ToUpper and ToLower methods respectively convert strings to upper or lower case.

 

Formatting Strings

The Format method allows you to define a longer string containing the values of numbers, objects or other strings.  The vbCRLF constant is the carriage return/line feed character used to insert a new line in a string.

 

Trimming and Removing Characters

 The Trim method removes any trailing white space in a string.

 

Padding Strings

The PadLeft and PadRight methods allow you to add new characters to a given string.  The PadLeft(10, "*") example here pads the first name "John" with six asterisks to the left.  The ten indicates the total length of the new string. 

 

 

posted @ 2015-06-17 15:16  xymum  阅读(287)  评论(0编辑  收藏  举报