c string two ways to remove Spaces of removes both ends of Spaces
- 2020-06-12 10:30:55
- OfStack
Ways to use strings:
trim (); Whitespace is removed from the string
split (); cutting
string. join (); The connection
class Program
{
static void Main(string[] args)
{
// The original string
string str = " hello world, you good The world ! ";
// Remove both ends of the space
str= str.Trim();
// Space cut
string [] strArray= str.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
// Connect with Spaces
string newStr= string.Join(" ", strArray);
Console.WriteLine(newStr);
Console.ReadKey();
}
}
Use the regular method:
class Program
{
static void Main(string[] args)
{
// The original string
string str = " hello world, you good The world ! ";
string s = Regex.Replace(str, @"\s+", " ").Trim();
Console.WriteLine(s);
Console.ReadKey();
}
}