VB

pecial character constants (all also accessible from ControlChars class)
vbCrLf, vbCr, vbLf, vbNewLine 
vbNullString 
vbTab 
vbBack 
vbFormFeed 
vbVerticalTab
""

' String concatenation (use & or +) 
Dim school As String = "Harding" & vbTab
school = school & "University" ' school is "Harding (tab) University"

' Chars
Dim letter As Char = school.Chars(0)   ' letter is H 
letter = "Z"c                                         ' letter is Z 
letter = Convert.ToChar(65)                ' letter is A 
letter = Chr(65)                                 ' same thing 
Dim word() As Char = school.ToCharArray() ' word holds Harding

' No string literal operator 
Dim msg As String = "File is c:\temp\x.dat" 

' String comparison
Dim mascot As String = "Bisons"
If (mascot = "Bisons") Then   ' true
If (mascot.Equals("Bisons")) Then   ' true
If (mascot.ToUpper().Equals("BISONS")) Then  ' true
If (mascot.CompareTo("Bisons") = 0) Then   ' true

' String matching with Like - Regex is more powerful
If ("John 3:16" Like "Jo[Hh]? #:*") Then   'true

' Substring
s = mascot.Substring(2, 3)) ' s is "son"

' Replacement
s = mascot.Replace("sons", "nomial")) ' s is "Binomial"

' Split
Dim names As String = "Michael,Dwight,Jim,Pam"
Dim parts() As String = names.Split(",".ToCharArray())   ' One name in each slot

' Date to string
Dim dt As New DateTime(1973, 10, 12)
Dim s As String = "My birthday: " & dt.ToString("MMM dd, yyyy")   ' Oct 12, 1973

' Integer to String
Dim x As Integer = 2
Dim y As String = x.ToString()     ' y is "2"

' String to Integer
Dim x As Integer = Convert.ToInt32("-5")     ' x is -5


' Mutable string 
Dim buffer As New System.Text.StringBuilder("two ")
buffer.Append("three ")
buffer.Insert(0, "one ")
buffer.Replace("two", "TWO")
Console.WriteLine(buffer)         ' Prints "one TWO three"

C#

Escape sequences
\r    // carriage-return
\n    // line-feed
\t    // tab
\\    // backslash
\"    // quote 

// String concatenation
string school = "Harding\t"; 
school = school + "University";   // school is "Harding (tab) University"

// Chars
char letter = school[0];            // letter is H 
letter = 'Z';                               // letter is Z 
letter = Convert.ToChar(65);     // letter is A 
letter = (char)65;                    // same thing 
char[] word = school.ToCharArray();   // word holds Harding

// String literal 
string msg = @"File is c:\temp\x.dat"; 
// same as 
string msg = "File is c:\\temp\\x.dat"; 

// String comparison
string mascot = "Bisons"; 
if (mascot == "Bisons")    // true
if (mascot.Equals("Bisons"))   // true
if (mascot.ToUpper().Equals("BISONS"))   // true
if (mascot.CompareTo("Bisons") == 0)    // true

// String matching - No Like equivalent, use Regex


// Substring
s = mascot.Substring(2, 3))     // s is "son"

// Replacement
s = mascot.Replace("sons", "nomial"))     // s is "Binomial"

// Split
string names = "Michael,Dwight,Jim,Pam";
string[] parts = names.Split(",".ToCharArray());   // One name in each slot

// Date to string
DateTime dt = new DateTime(1973, 10, 12);
string s = dt.ToString("MMM dd, yyyy");     // Oct 12, 1973

// int to string
int x = 2;
string y = x.ToString();     // y is "2"

// string to int
int x = Convert.ToInt32("-5");     // x is -5

 

// Mutable string 
System.Text.StringBuilder buffer = new System.Text.StringBuilder("two "); 
buffer.Append("three "); 
buffer.Insert(0, "one "); 
buffer.Replace("two", "TWO"); 
Console.WriteLine(buffer);     // Prints "one TWO three"

莊子 發表在 痞客邦 PIXNET 留言(0) 人氣()