Created
June 19, 2013 16:31
-
-
Save ricardodantas/5815703 to your computer and use it in GitHub Desktop.
Search string in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment