I figured I would write this little tutorial about C# and Regex since it is still fresh in my mind.
The code below shows how to remove all spaces from a string:
Code:using System; using System.Collections.Generic; using System.Text; namespace regexconsole { class Program { static void Main(string[] args) { // Create a string and display it to the console string display = "I have several spaces in me"; Console.WriteLine(display); // Replaces all spaces with 1 space display = System.Text.RegularExpressions.Regex.Replace(display," +", " "); Console.WriteLine(display); // Make it pause Console.ReadLine(); } } }
Here is how to determine if a string is a number or not
Code:using System; using System.Collections.Generic; using System.Text; namespace regexconsole { class Program { static void Main(string[] args) { // Create an integer and display it string number = "64"; Console.WriteLine(number); // Determine if it is a positive number if (System.Text.RegularExpressions.Regex.IsMatch(number, ("[0-9]*"))) { Console.WriteLine("This is a number"); } else { Console.WriteLine("This is not a number!"); } // Make it pause Console.ReadLine(); } } }
That is it for now. I will post more later.


LinkBack URL
About LinkBacks




Reply With Quote



Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum