VB.Net Tutorial/Data Type/String Functions
Версия от 16:40, 26 мая 2010; (обсуждение)
Содержание
- 1 CStr: Convert Double to String
- 2 Duplicate a string
- 3 Format: c
- 4 Get string index by using InStr
- 5 Instr
- 6 Instr: find character in a string
- 7 Instr: find sub string a in a string
- 8 IsDBNull
- 9 IsNumeric, IsDate, IsNothing
- 10 LCase: change string to lower case
- 11 Left: get characters from left
- 12 Len: get string length
- 13 Microsoft.VisualBasic.Left
- 14 Mid(string, 4, 5): get sub string from middle
- 15 Mid(string, 7): get sub string from start
- 16 Passing formatted numeric values to Val
- 17 Passing String values to Val
- 18 Right: get characters from right
- 19 String.Copy
- 20 String handling function: Len
- 21 UCase: change string to upper case
- 22 Use String.Concat to concatenate two strings
CStr: Convert Double to String
Public Class Tester
Public Shared Sub Main
Dim x As Double
x = 12345678901234567890D
Console.WriteLine(CStr(x))
End Sub
End Class
1.23456789012346E+19
Duplicate a string
Public Class Tester
Public Shared Sub Main
Console.WriteLine(StrDup(30, "-"))
End Sub
End Class
------------------------------
Format: c
Public Class Tester
Public Shared Sub Main
Console.WriteLine( Format(123, "c") )
End Sub
End Class
$123.00
Get string index by using InStr
Class Tester
Shared Sub Main()
Dim userInput As String
userInput = " asdf "
Console.WriteLine(InStr(userInput, "f"))
End Sub
End Class
6
Instr
Public Class Tester
Public Shared Sub Main
Dim MyString As String, YourString As String
MyString = "asdf"
YourString = "fdsa"
Console.WriteLine(InStr(2, MyString, YourString))
End Sub
End Class
0
Instr: find character in a string
Module Tester
Public Sub Main()
Dim sHello As String = "Hello World"
Console.WriteLine("Original string: {0}", sHello)
Console.WriteLine("Find character (Instr(1, sHello, "l")) : {0}", InStr(1, sHello, "l"))
End Sub
End Module
Original string: Hello World Find character (Instr(1, sHello, "l")) : 3
Instr: find sub string a in a string
Module Tester
Public Sub Main()
Dim sHello As String = "Hello World"
Console.WriteLine("Original string: {0}", sHello)
Console.WriteLine("Find substring (Instr(1, sHello, "World")) : {0}", InStr(1, sHello, "Wor
ld"))
End Sub
End Module
Original string: Hello World Find substring (Instr(1, sHello, "World")) : 7
IsDBNull
Public Module modMain
Public Sub Main()
Dim title As String = "Mr."
Dim name As Object = DBNull.Value
Dim addressLine1 As String = title & " " & name
If IsDBNull(addressLine1) Then
Console.WriteLine("Null value")
Else
Console.WriteLine(addressLine1)
End If
End Sub
End Module
Mr.
IsNumeric, IsDate, IsNothing
Public Class Tester
Public Shared Sub Main
Dim theData As String = Nothing
Dim result As New System.Text.StringBuilder
" ----- Format nothing.
result.AppendLine(String.Format( _
"IsNumeric({0}) ... {1}", theData, IsNumeric(theData)))
result.AppendLine(String.Format( _
"IsDate({0}) ... {1}", theData, IsDate(theData)))
result.AppendLine(String.Format( _
"IsNothing({0}) ... {1}", theData, IsNothing(theData)))
result.AppendLine()
" ----- Format a number in a string.
theData = "-12.345"
result.AppendLine(String.Format( _
"IsNumeric({0}) ... {1}", theData, IsNumeric(theData)))
result.AppendLine(String.Format( _
"IsDate({0}) ... {1}", theData, IsDate(theData)))
result.AppendLine(String.Format( _
"IsNothing({0}) ... {1}", theData, IsNothing(theData)))
result.AppendLine()
" ----- Format a date in a string.
theData = "July 17, 2007"
result.AppendLine(String.Format( _
"IsNumeric({0}) ... {1}", theData, IsNumeric(theData)))
result.AppendLine(String.Format( _
"IsDate({0}) ... {1}", theData, IsDate(theData)))
result.Append(String.Format( _
"IsNothing({0}) ... {1}", theData, IsNothing(theData)))
Console.WriteLine(result.ToString())
End Sub
End Class
IsNumeric() ... False IsDate() ... False IsNothing() ... True IsNumeric(-12.345) ... True IsDate(-12.345) ... False IsNothing(-12.345) ... False IsNumeric(July 17, 2007) ... False IsDate(July 17, 2007) ... True IsNothing(July 17, 2007) ... False
LCase: change string to lower case
Module Tester
Public Sub Main()
Dim sHello As String = "Hello World"
Console.WriteLine("Original string: {0}", sHello)
Console.WriteLine("Lowercase (LCase(sHello)) : {0}", LCase(sHello))
End Sub
End Module
Original string: Hello World Lowercase (LCase(sHello)) : hello world
Left: get characters from left
Module Tester
Public Sub Main()
Dim sHello As String = "Hello World"
Console.WriteLine("Original string: {0}", sHello)
Console.WriteLine("Leftmost two characters (Left(sHello, 2)): {0}", Left(sHello, 2))
End Sub
End Module
Original string: Hello World Leftmost two characters (Left(sHello, 2)): He
Len: get string length
Public Class Tester
Public Shared Sub Main
Console.WriteLine(Len("txtString1.Text"))
End Sub
End Class
15
Microsoft.VisualBasic.Left
public class Test
public Shared Sub Main
Dim strName As String = "Visual Basic Express"
If Microsoft.VisualBasic.Left(strName, 3) = "Vis" Then Console.WriteLine("True")
If strName.StartsWith("Vis") Then Console.WriteLine("True")
End Sub
End class
True True
Mid(string, 4, 5): get sub string from middle
Module Tester
Public Sub Main()
Dim sHello As String = "Hello World"
Console.WriteLine("Original string: {0}", sHello)
Console.WriteLine("From middle of string (Mid(sHello, 4, 5)): {0}", Mid(sHello, 4, 5))
End Sub
End Module
Original string: Hello World From middle of string (Mid(sHello, 4, 5)): lo Wo
Mid(string, 7): get sub string from start
Module Tester
Public Sub Main()
Dim sHello As String = "Hello World"
Console.WriteLine("Original string: {0}", sHello)
Console.WriteLine("Starting with character (Mid(sHello, 7)): {0}", Mid(sHello, 7))
End Sub
End Module
Original string: Hello World Starting with character (Mid(sHello, 7)): World
Passing formatted numeric values to Val
Option Strict On
Public Module Tester
Public Sub Main()
" Passing formatted numeric values to Val
Console.WriteLine(Val("$1034.51"))
Console.WriteLine(Val("1,032.54"))
End Sub
End Module
0 1
Passing String values to Val
Option Strict On
Public Module Tester
Public Sub Main()
"
Console.WriteLine(Val("913.3"))
Console.WriteLine(Val("131AC57"))
Console.WriteLine(Val("-132.23"))
Console.WriteLine(Val("+1203.22"))
Console.WriteLine(Val("A0545"))
Console.WriteLine()
End Sub
End Module
913.3 131 -132.23 1203.22 0
Right: get characters from right
Module Tester
Public Sub Main()
Dim sHello As String = "Hello World"
Console.WriteLine("Original string: {0}", sHello)
Console.WriteLine("Rightmost four characters (Right(sHello, 4)): {0}", Right(sHello, 4))
End Sub
End Module
Original string: Hello World Rightmost four characters (Right(sHello, 4)): orld
String.Copy
public class Test
public Shared Sub Main
Dim str1, str2 As String
str1 = "some text"
str2 = str1
str2 = String.Copy(str1)
Console.WriteLine(str1 & vbCrLf & str2)
End Sub
End class
some text some text
String handling function: Len
Module Tester
Public Sub Main()
Dim sHello As String = "Hello World"
Console.WriteLine("Original string: {0}", sHello)
Console.WriteLine("Length of string (Len(sHello)): {0}", Len(sHello))
End Sub
End Module
Original string: Hello World Length of string (Len(sHello)): 11
UCase: change string to upper case
Module Tester
Public Sub Main()
Dim sHello As String = "Hello World"
Console.WriteLine("Original string: {0}", sHello)
Console.WriteLine("Uppercase (UCase(sHello)) : {0}", UCase(sHello))
End Sub
End Module
Original string: Hello World Uppercase (UCase(sHello)) : HELLO WORLD
Use String.Concat to concatenate two strings
public class Test
public Shared Sub Main
Dim aryStrings() As String = {"string1", "string2", "string3", "string4"}
Dim strLongString As String
strLongString = String.Concat(aryStrings)
Console.WriteLine(strLongString)
End Sub
End class
string1string2string3string4