1 2 3 4 5 6 7 8 9 10 11 12 |
using System; public class StringToCharArray { public static void Main() { string s = "Test String"; for (int index = 0; index < s.Length; index++) Console.WriteLine("Char: {0}", s[index]); } } |
Data Types
Use Substring() 2
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Use Substring(). using System; public class SubstringDemo { public static void Main() { string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; Console.WriteLine("str: " + str); Console.Write("str.Substring(15): "); string substr = str.Substring(15); Console.WriteLine(substr); Console.Write("str.Substring(0, 15): "); substr = str.Substring(0, 15); Console.WriteLine(substr); } }
String Manipulation Concatenate
/* Learning C# by Jesse Liberty Publisher: O'Reilly ISBN: 0596003765 */ using System; namespace StringManipulation { public class TesterStringManipulationConcat { public void Run() { string s1 = "abcd"; string s2 = "ABCD"; // concatenation method string s3 = string.Concat(s1,s2); Console.WriteLine( "s3 concatenated from s1 and s2: {0}", s3); // use the overloaded operator string s4 = s1 + s2; Console.WriteLine( "s4 concatenated from s1 + s2: {0}", s4); } static void Main() { TesterStringManipulationConcat t = new TesterStringManipulationConcat(); t.Run(); } } }
String Manipulation
/* Learning C# by Jesse Liberty Publisher: O'Reilly ISBN: 0596003765 */ using System; namespace StringManipulation { public class TesterStringManipulationCompare { public void Run() { // create some strings to work with string s1 = "abcd"; string s2 = "ABCD"; int result; // hold the results of comparisons // compare two strings, case sensitive result = string.Compare(s1, s2); Console.WriteLine( "compare s1: {0}, s2: {1}, result: {2} ", s1, s2, result); // overloaded compare, takes boolean "ignore case" //(true = ignore case) result = string.Compare(s1,s2, true); Console.WriteLine("Compare insensitive. result: {0} ", result); } [STAThread] static void Main() { TesterStringManipulationCompare t = new TesterStringManipulationCompare(); t.Run(); } } }
Illustrates the use of strings 1
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example2_9.cs illustrates the use of strings */ public class Example2_9 { public static void Main() { string helloWorld = "Hello World!"; System.Console.WriteLine(helloWorld); helloWorld = "Hello World" + " from C#!"; System.Console.WriteLine(helloWorld); helloWorld = "Hello World" + " from C#!"; System.Console.WriteLine(helloWorld); const double Pi = 3.14159; System.Console.WriteLine("Pi = " + Pi); } }
A string can control a switch statement
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // A string can control a switch statement. using System; public class StringSwitch { public static void Main() { string[] strs = { "one", "two", "three", "two", "one" }; foreach(string s in strs) { switch(s) { case "one": Console.Write(1); break; case "two": Console.Write(2); break; case "three": Console.Write(3); break; } } Console.WriteLine(); } }
Use Substring() 1
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Use Substring(). using System; public class SubStr { public static void Main() { string orgstr = "C# makes strings easy."; // construct a substring string substr = orgstr.Substring(5, 12); Console.WriteLine("orgstr: " + orgstr); Console.WriteLine("substr: " + substr); } }