このヘルプでは、COBOL、C#、VB.NET の間にある文字列の処理方法の相違点について説明します。
Escape sequences x"0a" *> line-feed x"09" *> tab "\" *> backslash "" *> quote *> string concatenation 01 school string value "Harding" & x"09". set school to string::Concat(school, "University") *> school is "Harding (tab) University" *> Chars 01 letter character. 01 word character occurs any. set letter to school::Chars(0) *> letter is H set letter to type Convert::ToChar(65) *> letter is A set letter to 65 as character *> same thing set word to school::ToCharArray *>word holds Harding *> String literal 01 msg string value "File is c:\temp\x.dat". *>String comparison 01 mascot string value "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 set s to mascot::Substring(2 3) *> s is "son" *> Replacement set s to mascot::Replace("sons" "nomial") *> s is "Binomial" *> Split 01 names string value "Frank,Becky,Ethan,Braden". 01 parts string occurs any. set parts to names::Split(",") *> Date to string 01 dt type DateTime value new DateTime(1973, 10, 12). 01 s string. set s to dt::ToString("MMM dd, yyyy") *> Oct 12, 1973 *> int to string 01 x string. 01 y binary-long value 2. set x to type x::ToString *> x is "2" *> string to int 01 x binary-long. set x to type Convert::ToInt32("-5") *> x is -5 *> Mutable string 01 buffer type System.Text.StringBuilder value new System.Text.StringBuilder("two "). invoke buffer::Append("three ") invoke buffer::Insert(0, "one ") invoke buffer::Replace("two" "TWO" display buffer *> Prints "one TWO three"
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 // 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 = "Frank,Becky,Ethan,Braden"; // One name in each slot string[] parts = names.Split(",".ToCharArray()); // 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"
Special 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 = 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 = "Frank,Becky,Ethan,Braden" 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"
これらの例の一部は、ハーディング大学コンピュータ サイエンス学部の Frank McCown 博士が作成したもので、クリエイティブ コモンズ ライセンスに基づいて使用が許可されています。