/* Learning C# by Jesse Liberty Publisher: O'Reilly ISBN: 0596003765 */ using System; using System.Text; namespace StringSearch { public class TesterStringSearch1 { public void Run() { // create some strings to work with string s1 = "One,Two,Three Liberty Associates, Inc."; // constants for the space and comma characters const char Space = ' '; const char Comma = ','; // array of delimiters to split the sentence with char[] delimiters = new char[] { Space, Comma }; // use a StringBuilder class to build the // output string StringBuilder output = new StringBuilder(); int ctr = 1; // split the string and then iterate over the // resulting array of strings foreach (string subString in s1.Split(delimiters)) { // AppendFormat appends a formatted string output.AppendFormat("{0}: {1} ",ctr++,subString); } Console.WriteLine(output); } [STAThread] static void Main() { TesterStringSearch1 t = new TesterStringSearch1(); t.Run(); } } }
Data Types
String search Demo
/* Learning C# by Jesse Liberty Publisher: O'Reilly ISBN: 0596003765 */ using System; namespace StringSearch { public class TesterStringSearch { public void Run() { // create some strings to work with string s1 = "One,Two,Three Liberty Associates, Inc. "; // constants for the space and comma characters const char Space = ' '; const char Comma = ','; // array of delimiters to split the sentence with char[] delimiters = new char[] { Space, Comma }; string output = ""; int ctr = 1; // split the string and then iterate over the // resulting array of strings String[] resultArray = s1.Split(delimiters); foreach (String subString in resultArray) { output += ctr++; output += ": "; output += subString; output += " "; } Console.WriteLine(output); } static void Main() { TesterStringSearch t = new TesterStringSearch(); t.Run(); } } }
String search: last index
/* Learning C# by Jesse Liberty Publisher: O'Reilly ISBN: 0596003765 */ using System; namespace StringSearch { public class TesterStringLastIndex { public void Run() { // create some strings to work with string s1 = "One Two Three Four"; int index; // get the index of the last space index=s1.LastIndexOf(" "); // get the last word. string s2 = s1.Substring(index+1); // set s1 to the substring starting at 0 // and ending at index (the start of the last word // thus s1 has one two three s1 = s1.Substring(0,index); // find the last space in s1 (after two) index = s1.LastIndexOf(" "); // set s3 to the substring starting at // index, the space after "two" plus one more // thus s3 = "three" string s3 = s1.Substring(index+1); // reset s1 to the substring starting at 0 // and ending at index, thus the string "one two" s1 = s1.Substring(0,index); // reset index to the space between // "one" and "two" index = s1.LastIndexOf(" "); // set s4 to the substring starting one // space after index, thus the substring "two" string s4 = s1.Substring(index+1); // reset s1 to the substring starting at 0 // and ending at index, thus "one" s1 = s1.Substring(0,index); // set index to the last space, but there is // none so index now = -1 index = s1.LastIndexOf(" "); // set s5 to the substring at one past // the last space. there was no last space // so this sets s5 to the substring starting // at zero string s5 = s1.Substring(index+1); Console.WriteLine ("s2: {0} s3: {1}",s2,s3); Console.WriteLine ("s4: {0} s5: {1} ",s4,s5); Console.WriteLine ("s1: {0} ",s1); } static void Main() { TesterStringLastIndex t = new TesterStringLastIndex(); t.Run(); } } }
use the IndexOf() and LastIndexOf() methods to search for substrings and characters;
using System; class MainClass { public static void Main() { string[] myStrings = {"To", "be", "or", "not","to", "be"}; string myString = String.Join(".", myStrings); int index = myString.IndexOf("be"); Console.WriteLine(""be" first occurs at index " + index + " of myString"); index = myString.LastIndexOf("be"); Console.WriteLine(""be" last occurs at index " + index + " of myString"); index = myString.IndexOf('b'); Console.WriteLine("'b' first occurs at index " + index + " of myString"); index = myString.LastIndexOf('b'); Console.WriteLine("'b' last occurs at index " + index + " of myString"); } }
Search strings
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Search strings. using System; public class StringSearchDemo { public static void Main() { string str = "C# has powerful string handling."; int idx; Console.WriteLine("str: " + str); idx = str.IndexOf('h'); Console.WriteLine("Index of first 'h': " + idx); idx = str.LastIndexOf('h'); Console.WriteLine("Index of last 'h': " + idx); idx = str.IndexOf("ing"); Console.WriteLine("Index of first "ing": " + idx); idx = str.LastIndexOf("ing"); Console.WriteLine("Index of last "ing": " + idx); char[] chrs = { 'a', 'b', 'c' }; idx = str.IndexOfAny(chrs); Console.WriteLine("Index of first 'a', 'b', or 'c': " + idx); if(str.StartsWith("C# has")) Console.WriteLine("str begins with "C# has""); if(str.EndsWith("ling.")) Console.WriteLine("str ends with "ling.""); } }
remove any of a set of chars from a given string.
using System; public class Class1 { public static void Main(string[] strings) { char[] cSpecialChars = { ' ', ',', '_' }; string s = "_This is, a str ing"; Console.WriteLine(RemoveSpecialChars(s, cSpecialChars)); } public static string RemoveSpecialChars(string sInput,char[] cTargets) { string sOutput = sInput; foreach (char c in cTargets) { for (; ; ) { int nOffset = sOutput.IndexOf(c); if (nOffset == -1) { break; } string sBefore = sOutput.Substring(0, nOffset); string sAfter = sOutput.Substring(nOffset + 1); sOutput = sBefore + sAfter; } } return sOutput; } }
String insert and output
using System; public class StringTest { public static void Main() { string test1 = "This is a test string"; string test2, test3; test2 = test1.Insert(15, "application "); test3 = test1.ToUpper(); Console.WriteLine("test1: '{0}'", test1); Console.WriteLine("test2: '{0}'", test2); Console.WriteLine("test3: '{0}'", test3); if (test1 == test3) Console.WriteLine("test1 is equal to test3"); else Console.WriteLine("test1 is not equal to test3"); test2 = test1.Replace("test", "sample"); Console.WriteLine("the new test2: '{0}'", test2); } }