为什么没有像String.Empty那样的Char.Empty?
这是有原因的吗?我这样问是因为如果您需要使用大量空字符,那么您会遇到与使用大量空字符串时相同的情况。
编辑:这种用法的原因是这样的:
myString.Replace ('c', '')
因此从 myString
中删除 'c'
的所有实例。
Is there a reason for this? I am asking because if you needed to use lots of empty chars then you get into the same situation as you would when you use lots of empty strings.
Edit: The reason for this usage was this:
myString.Replace ('c', '')
So remove all instances of 'c'
from myString
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(23)
从字符串中全面删除字符的最简单方法是修剪它
cl = cl.Trim(' ');
删除字符串中的所有空格
Easiest way to blanket remove a character from string is to Trim it
cl = cl.Trim(' ');
Removes all of the spaces in a string
使用
use
BOM 怎么样,微软添加到文件开头(至少是 XML)的神奇字符?
How about BOM, the magical character Microsoft adds to start of files (at least XML)?
我们是这样做的:
myString.Replace('''.ToString(), "");
here is how we do it :
myString.Replace ('''.ToString(), "");
如果你想消除字符串中的空字符,下面的方法就可以了。只需转换为您想要的任何数据类型表示形式即可。
If you want to eliminate the empty char in string, the following will work. Just convert to any datatype representation you want.
就C#语言而言,以下内容可能没有多大意义。这并不是问题的直接答案。但以下是我在一个业务场景中所做的事情。
null
和white space
在我的项目中具有不同的业务流程。在插入数据库时,如果它是空格,我需要将空字符串
插入到数据库中。In terms of C# language, the following may not make much sense. And this is not a direct answer to the question. But following is what I did in one of my business scenarios.
The
null
andwhite space
had different business flows in my project. While inserting into database, I need to insertempty string
to the database if it is white space.如果要删除满足特定条件的字符,可以使用以下命令:(
这将只保留字符串中的大写字符。)
换句话说,您可以将字符串“使用”为
IEnumerable。
,对其进行更改,然后将其转换回字符串,如上所示。同样,这不仅可以因为 lambda 表达式而删除特定的字符,尽管如果您像这样更改 lambda 表达式也可以这样做: c != 't'。
If you want to remove characters that satisfy a specific condition, you may use this:
(This will leave only the uppercase characters in the string.)
In other words, you may "use" the string as an
IEnumerable<char>
, make changes on it and then convert it back to a string as shown above.Again, this enables to not only remove a specific char because of the lambda expression, although you can do so if you change the lambda expression like this:
c => c != 't'
.我知道这个已经很旧了,但我最近遇到了一个问题,必须进行多次替换才能确保文件名安全。首先,在最新的.NET string.Replace函数中,null相当于空字符。话虽如此,.Net 缺少的是一个简单的替换全部功能,它将用所需的字符替换数组中的任何字符。请随意参考下面的代码(在 LinqPad 中运行进行测试)。
I know this one is pretty old, but I encountered an issue recently with having to do multiple replacements to make a file name safe. First, in the latest .NET string.Replace function null is the equivalent to empty character. Having said that, what is missing from .Net is a simple replace all that will replace any character in an array with the desired character. Please feel free to reference the code below (runs in LinqPad for testing).
您还可以逐个字符地重建字符串,不包括要删除的字符。
这是执行此操作的扩展方法:
它并不圆滑或花哨,但效果很好。
像这样使用:
You can also rebuild your string character by character, excluding the characters that you want to get rid of.
Here's an extension method to do this:
It's not slick or fancy, but it works well.
Use like this:
如果您查看 Replace(String, String) 重载 您可以在备注部分看到,如果第二个参数是
null
那么它将删除您指定的所有实例。因此,如果您仅使用myString = myString.Replace("c", null);
,它将删除字符串中的每个c
。If you look at the documentation for Replace(String, String) overload for String.Replace you can see in the remarks section that if the second argument is a
null
then it will remove all instances of what you specified. Therefore if you just usemyString = myString.Replace("c", null);
it will delete everyc
in the string.没有回答您的第一个问题 - 但对于您遇到的具体问题,您可以使用字符串而不是字符,对吧?:
您有理由不想这样做吗?
Doesn't answer your first question - but for the specific problem you had, you can just use strings instead of chars, right?:
There a reason you wouldn't want to do that?
char 是值类型,因此它的值不能为 null。 (除非它包装在 Nullable 容器中)。
由于它不能为空,因此 in 包含一些数字代码,并且每个代码都映射到某个字符。
A char is a value type, so its value cannot be null. (Unless it is wrapped in a Nullable container).
Since it can't be null, in contains some numeric code and each code is mapped to some character.
与没有
int.Empty
的原因相同。容器可以为空,但标量值不能为空。如果您的意思是 0(不为空),则使用'\0'
。如果您的意思是null
,则使用null
:)The same reason there isn't an
int.Empty
. Containers can be empty, scalar values cannot. If you mean 0 (which is not empty), then use'\0'
. If you meannull
, then usenull
:)好的,这对于删除字母来说并不是特别优雅,因为 .Replace 方法有一个采用字符串参数的重载。但这适用于删除回车符、换行符、制表符等。此示例删除制表符:
OK, this is not particularly elegant for removing letters, since the .Replace method has an overload that takes string parameters. But this works for removing carriage returns, line feeds, tabs, etc. This example removes tab characters:
如果您不需要整个字符串,您可以利用延迟执行:
您甚至可以组合多个字符...
...并且只会评估前 7 个字符:)
编辑:或者,甚至更好:
和它的用法:
If you don't need the entire string, you can take advantage of the delayed execution:
You can even combine multiple characters...
... and only the first 7 characters will be evaluated :)
EDIT: or, even better:
and its usage:
不是您问题的答案,但要表示默认的
char
,您可以使用与
char.MinValue
相同的值,而char.MinValue
又与\0< /代码>。不过,人们不应该将它用于诸如空字符串之类的东西。
Not an answer to your question, but to denote a default
char
you can use justwhich is same as
char.MinValue
which in turn is same as\0
. One shouldn't use it for something like an empty string though.您可以使用 可空 字符。
You could use nullable chars.
使用
Char.MinValue
其作用与“\0”相同。但要注意它与String.Empty
不同。Use
Char.MinValue
which works the same as '\0'. But be careful it is not the same asString.Empty
.不存在所谓的空字符。它总是包含某些东西。甚至“\0”也是一个字符。
There's no such thing as an empty character. It always contains something. Even '\0' is a character.
与字符串不同,字符是具有固定大小的离散事物。字符串实际上是字符的容器。
因此,Char.Empty 在这种情况下并没有真正的意义。如果你有一个字符,它就不是空的。
A char, unlike a string, is a discrete thing with a fixed size. A string is really a container of chars.
So, Char.Empty doesn't really make sense in that context. If you have a char, it's not empty.
要从字符串中删除特定字符,您可以使用字符串重载:
您的语句
不会删除任何字符。它只会将它们替换为
'\0'
(字符串结尾,EOS),并产生不同的结果。某些字符串操作可能会在遇到 EOS 时停止,但在 .NET 中,大多数操作会将其视为任何其他字符。最好尽可能避免'\0'
。To remove a specific char from a string you can use the string overload:
Your statement
Won't remove any characters. It will just replace them with
'\0'
(End-Of-String, EOS), with varying consequences. Some string operations might stop when encountering an EOS but in .NET most actions will treat it like any other char. Best to avoid'\0'
as much as possible.不存在空字符这样的东西。您能得到的最接近的是
'\0'
,即 Unicode“null”字符。鉴于您可以将其嵌入到字符串文字中或非常容易地单独表达它,为什么您需要一个单独的字段呢?同样,“很容易混淆””
和” “
” 参数不适用于'\0'
。如果你能举一个例子来说明你想在哪里使用它以及为什么你认为它会更好,这可能会有所帮助......
There's no such thing as an empty char. The closest you can get is
'\0'
, the Unicode "null" character. Given that you can embed that within string literals or express it on its own very easily, why would you want a separate field for it? Equally, the "it's easy to confuse""
and" "
" arguments don't apply for'\0'
.If you could give an example of where you'd want to use it and why you think it would be better, that might help...