using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.IO; using System.Reflection; using System.Linq; using System.Xml; using System.Xml.Linq; public static class Extensions { public static string SpaceToUnderscore(this string source) { char[] cArray = source.ToCharArray(); string result = null; foreach (char c in cArray) { if (Char.IsWhiteSpace(c)) result += "_"; else result += c; } return result; } } class Program { static void Main(string[] args) { string s = "This is a test"; Console.WriteLine(s.SpaceToUnderscore()); } }