/* 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(); } }
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
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); } }
Display the digits of an integer using words
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Display the digits of an integer using words. using System; public class ConvertDigitsToWords { public static void Main() { int num; int nextdigit; int numdigits; int[] n = new int[20]; string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; num = 1908; Console.WriteLine("Number: " + num); Console.Write("Number in words: "); nextdigit = 0; numdigits = 0; /* Get individual digits and store in n. These digits are stored in reverse order. */ do { nextdigit = num % 10; n[numdigits] = nextdigit; numdigits++; num = num / 10; } while(num > 0); numdigits--; // display words for( ; numdigits >= 0; numdigits--) Console.Write(digits[n[numdigits]] + " "); Console.WriteLine(); } }
Demonstrate string arrays
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate string arrays.
using System;
public class StringArrays {
public static void Main() {
string[] str = { “This”, “is”, “a”, “test.” };
Console.WriteLine(“Original array: “);
for(int i=0; i < str.Length; i++)
Console.Write(str[i] + " ");
Console.WriteLine("
");
// change a string
str[1] = "was";
str[3] = "test, too!";
Console.WriteLine("Modified array: ");
for(int i=0; i < str.Length; i++)
Console.Write(str[i] + " ");
}
}
[/csharp]
Some string operations
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Some string operations.
using System;
public class StrOps {
public static void Main() {
string str1 =
“When it comes to .NET programming, C# is #1.”;
string str2 = string.Copy(str1);
string str3 = “C# strings are powerful.”;
string strUp, strLow;
int result, idx;
Console.WriteLine(“str1: ” + str1);
Console.WriteLine(“Length of str1: ” +
str1.Length);
// create upper- and lowercase versions of str1
strLow = str1.ToLower();
strUp = str1.ToUpper();
Console.WriteLine(“Lowercase version of str1:
” +
strLow);
Console.WriteLine(“Uppercase version of str1:
” +
strUp);
Console.WriteLine();
// display str1, one char at a time.
Console.WriteLine(“Display str1, one char at a time.”);
for(int i=0; i < str1.Length; i++)
Console.Write(str1[i]);
Console.WriteLine("
");
// compare strings
if(str1 == str2)
Console.WriteLine("str1 == str2");
else
Console.WriteLine("str1 != str2");
if(str1 == str3)
Console.WriteLine("str1 == str3");
else
Console.WriteLine("str1 != str3");
result = str1.CompareTo(str3);
if(result == 0)
Console.WriteLine("str1 and str3 are equal");
else if(result < 0)
Console.WriteLine("str1 is less than str3");
else
Console.WriteLine("str1 is greater than str3");
Console.WriteLine();
// assign a new string to str2
str2 = "One Two Three One";
// search string
idx = str2.IndexOf("One");
Console.WriteLine("Index of first occurrence of One: " + idx);
idx = str2.LastIndexOf("One");
Console.WriteLine("Index of last occurrence of One: " + idx);
}
}
[/csharp]
Introduce string
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Introduce string. using System; public class StringDemo { public static void Main() { char[] charray = {'A', ' ', 's', 't', 'r', 'i', 'n', 'g', '.' }; string str1 = new string(charray); string str2 = "Another string."; Console.WriteLine(str1); Console.WriteLine(str2); } }
Trimming String Spaces
/* * C# Programmers Pocket Consultant * Author: Gregory S. MacBeth * Email: gmacbeth@comporium.net * Create Date: June 27, 2003 * Last Modified Date: */ using System; namespace Client.Chapter_6___Strings { public class TrimmingSpaces { static void Main(string[] args) { string MyString = " Hello, World ! "; MyString.TrimStart(); Console.WriteLine(MyString); MyString.TrimEnd(); Console.WriteLine(MyString); MyString.Trim(char.Parse("!")); Console.WriteLine(MyString); } } }