Parse Strings Using the Split Method
class TestStringSplit
{
static void Main()
{
char[] delimiterChars = { ' ', ',', '.', ':', '\t' };
string text = "one\ttwo three:four,five six seven";
System.Console.WriteLine("Original text: '{0}'", text);
string[] words = text.Split(delimiterChars);
System.Console.WriteLine("{0} words in text:", words.Length);
foreach (string s in words)
{
System.Console.WriteLine(s);
}
}
}
Search Strings Using String Methods
class StringSearch
{
static void Main()
{
string str = "A silly sentence used for silly purposes.";
System.Console.WriteLine("'{0}'",str);
bool test1 = str.StartsWith("a silly");
System.Console.WriteLine("starts with 'a silly'? {0}", test1);
bool test2 = str.StartsWith("a silly", System.StringComparison.OrdinalIgnoreCase);
System.Console.WriteLine("starts with 'a silly'? {0} (ignoring case)", test2);
bool test3 = str.EndsWith(".");
System.Console.WriteLine("ends with '.'? {0}", test3);
int first = str.IndexOf("silly");
int last = str.LastIndexOf("silly");
string str2 = str.Substring(first, last - first);
System.Console.WriteLine("between two 'silly' words: '{0}'", str2);
}
}
Search Strings Using Regular Expressions
class TestRegularExpressions
{
static void Main()
{
string[] sentences =
{
"cow over the moon",
"Betsy the Cow",
"cowering in the corner",
"no match here"
};
string sPattern = "cow";
foreach (string s in sentences)
{
System.Console.Write("{0,24}", s);
if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
System.Console.WriteLine(" (match for '{0}' found)", sPattern);
}
else
{
System.Console.WriteLine();
}
}
}
}
class TestRegularExpressionValidation
{
static void Main()
{
string[] numbers =
{
"123-456-7890",
"444-234-22450",
"690-203-6578",
"146-893-232",
"146-839-2322",
"4007-295-1111",
"407-295-1111",
"407-2-5555",
};
string sPattern = "^\\d{3}-\\d{3}-\\d{4}$";
foreach (string s in numbers)
{
System.Console.Write("{0,14}", s);
if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern))
{
System.Console.WriteLine(" - valid");
}
else
{
System.Console.WriteLine(" - invalid");
}
}
}
}
Join Multiple Strings
The + operator is easy to use and makes for intuitive code, but it works in series; a new string is created for each use of the operator, so chaining multiple operators together is inefficient. For example:
string two = "two";
string str = "one " + two + " three";
System.Console.WriteLine(str);
Alternatively, the StringBuilder class can be used to add each string to an object that then creates the final string in one step. This strategy is demonstrated in the following example.
class StringBuilderTest
{
static void Main()
{
string two = "two";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("one ");
sb.Append(two);
sb.Append(" three");
System.Console.WriteLine(sb.ToString());
string str = sb.ToString();
System.Console.WriteLine(str);
}
}
Modify String Contents
The following example uses the ToCharArray method to extract the contents of a string into an array of the char type. Some of the elements of this array are then modified. The char array is then used to create a new string instance.
class ModifyStrings
{
static void Main()
{
string str = "The quick brown fox jumped over the fence";
System.Console.WriteLine(str);
char[] chars = str.ToCharArray();
int animalIndex = str.IndexOf("fox");
if (animalIndex != -1)
{
chars[animalIndex++] = 'c';
chars[animalIndex++] = 'a';
chars[animalIndex] = 't';
}
string str2 = new string(chars);
System.Console.WriteLine(str2);
}
}
No comments:
Post a Comment