如何删除字符C# [英] How to remove character c#

查看:73
本文介绍了如何删除字符C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的文本:今天,是美好的一天.明天会下雨!

现在我要删除. !和空间

结果是
TodayisagooddayTomorrowwillrain.

I have a text e.x like that: Today, is a good day. Tomorrow, will rain!

now I want to remove , . ! and Spaces

and the result to be
TodayisagooddayTomorrowwillrain.

推荐答案



您可以使用字符串函数Replace删除以下字符:
Hi,

You can use string function Replace to remove these characters:
string str="Today, is a good day. Tomorrow, will rain!";
str = str.Replace(",", "");
str = str.Replace(".", "");
str = str.Replace("!", "");
str = str.Replace(" ", "");


我认为System.Text.RegularExpressions.Regex类的Replace 方法可以用于此目的

I think the Replace method of System.Text.RegularExpressions.Regex class can be used for this purpose

string text = "Today, is     a good day. Tomorrow, will rain!";
//Remove punctuation characters and spaces
string textSansPunctuationSpace = System.Text.RegularExpressions.Regex.Replace(text,@"\p{P}|\s+","", RegexOptions.CultureInvariant);
Console.WriteLine (replacedText);
//Output:
//TodayisagooddayTomorrowwillrain


解决方案6很好.可能会尝试跟随,

Solution 6 is good. It might try following,

static void Main(string[] args)
{
    string str = "Today, is a good day. Tomorrow, will rain!";
    var result = new String(str.Where(ch => !Char.IsPunctuation(ch) && !Char.IsWhiteSpace(ch)).ToArray());
    Console.WriteLine(result);
}



:)



:)


这篇关于如何删除字符C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆