简体   繁体   English

用于空白的C#字符串存储

[英]C# string storage for white-space

I'm asking if there's a difference between the following two strings: 我问的是以下两个字符串之间是否有区别:

string s1 = "Hello World";
string s2 = "Hello" + " " + "World";

because "Hello" + " " + "World" is working for me with List functions like .Any() .Contains() and .Equals() whereas "Hello World" doesn't work for these functions here. 因为“Hello”+“”+“World”对我来说有List函数,如.Any()。CONTins()和.Equals(),而“Hello World”对这些函数不起作用。

the simple answer is that there is no difference! 简单的答案就是没有区别!

The IEnumerable extension methods (also called LINQ for objects) you mentioned (.Any() etc.) work on both strings as string implements IEnumerable<char> 您提到的IEnumerable扩展方法(也称为对象的LINQ)(.Any()等)在string实现时对两个字符串都有效IEnumerable<char>

example: 例:

string s1 = "Hello World";
string s2 = "Hello" + " " + "World";

Console.WriteLine(string.Concat(s1.Select(s => s.ToString())));
Console.WriteLine(string.Concat(s2.Select(s => s.ToString())));

output: 输出:

Hello World
Hello World

try it: https://dotnetfiddle.net/BKTMHj 试试吧: https//dotnetfiddle.net/BKTMHj

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM