String
____________________________________________________________________
VB.NET
Special character constants
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 = 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
Console.WriteLine(mascot.Substring(2, 3)) ' Prints "son"
' String matching
If ("John 3:16" Like "Jo[Hh]? #:*") Then 'true
Imports System.Text.RegularExpressions ' More powerful than Like
Dim r As New Regex("Jo[hH]. \d:*")
If (r.Match("John
' My birthday:
Dim dt As New DateTime(1973, 10, 12)
Dim s As String = "My birthday: " & dt.ToString("MMM dd, yyyy")
' 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 = 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
Console.WriteLine(mascot.Substring(2, 3)); // Prints "son"
// String matching
// No Like equivalent - use regular expressions
using System.Text.RegularExpressions;
Regex r = new Regex(@"Jo[hH]. \d:*");
if (r.Match("John
// My birthday:
DateTime dt = new DateTime(1973, 10, 12);
string s = "My birthday: " + dt.ToString("MMM dd, yyyy");
// 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"
-------------------------------------------------------------------------------------------------
Note: This code applicable for both desktop and web application.
No comments:
Post a Comment