简体   繁体   English

字符串中的String.Empty

[英]String.Empty in strings

2 days ago, there was a question related to string.LastIndexOf(String.Empty) returning the last index of string: 2天前,有一个与string.LastIndexOf(String.Empty)相关的问题,返回字符串的最后一个索引:

Do C# strings end with empty string? C#字符串是否以空字符串结尾?

So I thought that; 所以我想到了; a string can always contain string.empty between characters like: 一个字符串总是可以在字符之间包含string.empty,如:

"testing" == "t" + String.Empty + "e" + String.Empty +"sting" + String.Empty;

After this, I wanted to test if String.IndexOf(String.Empty) was returning 0 because since String.Empty can be between any char in a string, that would be what I expect it to return and I wasn't wrong. 在此之后,我想测试String.IndexOf(String.Empty)是否返回0,因为String.Empty可以在字符串中的任何字符之间,这将是我期望它返回的,我没有错。

string testString = "testing";
int index = testString.LastIndexOf(string.Empty); // index is 6
index = testString.IndexOf(string.Empty); // index is 0

It actually returned 0. I started to think that if I could split a string with String.Empty , I would get at least 2 string and those would be String.Empty and rest of the string since String.IndexOf(String.Empty) returned 0 and String.LastIndexOf(String.Empty) returned length of the string.. Here is what I coded: 它实际上返回0.我开始认为如果我可以用String.Empty拆分一个字符串,我会得到至少2个字符串,那将是String.Empty和字符串的其余部分,因为String.IndexOf(String.Empty)返回0和String.LastIndexOf(String.Empty)返回字符串的长度..这是我编码的:

string emptyString = string.Empty;
char[] emptyStringCharArr = emptyString.ToCharArray();
string myDummyString = "abcdefg";
string[] result = myDummyString.Split(emptyStringCharArr);

The problem here is, I can't obviously convert String.Empty to char[] and result in an empty string[]. 这里的问题是,我无法将String.Empty转换为char[]并导致空字符串[]。 I would really love to see the result of this operation and the reason behind this. 我真的很想看到这个操作的结果及其背后的原因。 So my questions are: 所以我的问题是:

  1. Is there any way to split a string with String.Empty ? 有没有办法用String.Empty拆分字符串?

  2. If it is not possible but in an absolute world which it would be possible, would it return an array full of chars like [0] = "t" [1] = "e" [2] = "s" and so on or would it just return the complete string? 如果不可能,但在一个绝对的世界中,它是否可能,它会返回一个充满字符的数组,如[0] = "t" [1] = "e" [2] = "s"等等或它只会返回完整的字符串吗? Which would make more sense and why? 这会更有意义,为什么?

You will always get an Index of 0 when you look for String.Empty in any String, because it's the definition of String.IndexOf(String.Empty) you should have a look at the MSDN , where it says: 当你在任何String中查找String.Empty时,你总是得到一个索引为0,因为它是String.IndexOf(String.Empty)的定义你应该看一下MSDN ,它说:

"The zero-based index position of value if that string is found, or -1 if it is not. If value is String.Empty, the return value is 0." “如果找到该字符串,则从零开始的索引值位置,如果不是,则返回-1。如果值为String.Empty,则返回值为0。”

Directed to your second Question: 针对你的第二个问题:

I think you can Split a String with an Empty String by doing something like this in your code: 我认为您可以通过在代码中执行以下操作来拆分带有空字符串的字符串:

String test = "fwewfeoj";
test.Split(new String[] { String.Empty }, StringSplitOptions.None);

By the way: Possible Clone of this answer Why does "abcd".StartsWith("") return true? 顺便说一句:可能克隆这个答案为什么“abcd”.StartsWith(“”)返回true?

是的,您可以使用字符串.Empty拆分任何字符串

 string[] strArr = s.Split(string.Empty.ToCharArray());

Do you really need to split the string, or are you just trying to get all the individual characters? 你真的需要分割字符串,还是只是试图获得所有单个字符?

If so, then a string is also a IEnumerable<char> , and you also have an indexer. 如果是这样,那么字符串也是IEnumerable<char> ,你也有一个索引器。

So, what are you actually trying to do? 那么,你究竟想做什么?

And no, you can't call the split methods with string.Empty or similar constructs. 不,你不能用string.Empty或类似的结构调用split方法。

string emptyString = string.Empty;
char[] emptyStringCharArr = emptyString.ToCharArray();

This will give you an empty array of chars. 这将为您提供一个空数组的字符。 This is because String is already an array of chars in memory, and String.Empty has no value. 这是因为String已经是内存中的字符数组,而String.Empty没有值。

To break it down further, consider an implementation of .ToCharArray() 要进一步细分,请考虑.ToCharArray()的实现

private Char[] toCharArray(String value)
{
    var stringLength = value.Length;
    var returningArray = new char[stringLength];
    for(var i = 0; i < stringLength; i++)
    {
        returningArray[i] = value[i];
    }
    return returningArray;
}

Length of course will be zero, and you will return an empty char array. 当然长度为零,您将返回一个空的char数组。 Of course this isn't the exact implementation, but you can see how and why it's returning nothing (and therefore isn't splitting on the string as you're expecting it to) 当然这不是确切的实现,但你可以看到它是如何以及为什么没有返回任何东西(因此不会像你期望的那样分裂字符串)

It's not an array with a single element String.Empty, because that doesn't really make sense. 它不是一个包含单个元素String.Empty的数组,因为这没有多大意义。 When you try to split on an empty array, it doesn't know how or what to split on, so you're given back the original string. 当您尝试拆分空数组时,它不知道如何拆分或拆分,因此您将返回原始字符串。

As for why it returns 0 by default, consider: 至于为什么它默认返回0,考虑:

private int IndexOf(String value, String searchFor)
{
    for(var i = 0; i < value.Length; i++)
    {
        if(value.Substring(i, searchFor.Length) == searchFor)
        {
            return i;
        }
    }
    return -1;
}
private int LastIndexOf(String value, String searchFor)
{
    var searchLength = searchFor.Length;
    for(var i = value.Length - searchFor.Length; i >= 0; i--)
    {
        if(value.Substring(i, searchLength) == searchFor)
            return i;
    }
    return -1;
}

String.SubString(x, 0) will ALWAYS return String.Empty, regardless of what's passed in (even String.Empty). String.SubString(x,0)将始终返回String.Empty,无论传入什么(甚至是String.Empty)。 For this reason it's much faster to add a check and return 0 regardless (as it would even if it ran the loop). 因此,添加检查并返回0会更快(因为即使它运行循环也是如此)。

Since String.Empty is just an empty string, so if you do: 由于String.Empty只是一个空字符串,所以如果你这样做:

var s = "part1" + string.Empty + "part2";

this will result in exactly the same string as: 这将导致完全相同的字符串:

var s = "part1" + "part2";

the first syntax will not insert a magic empty string between the two parts. 第一种语法不会在两个部分之间插入魔术空字符串。

That IndexOf returns 0, is by definition, not because there is some magic empty string between characters. 根据定义,IndexOf返回0,不是因为字符之间有一些魔术空字符串。

I cannot think of a logic way to split a string, by an empty string. 我想不出用空字符串分割字符串的逻辑方法。 What should it return? 应该归还什么? When using an empty string as an argument to the string.Split method, it will be ignored. 当使用空字符串作为string.Split方法的参数时,它将被忽略。 If it was the only separator to use, the string will be returned unsplit. 如果它是唯一使用的分隔符,则将返回未分割的字符串。

you could also say 你也可以说

"testing" == string.Empty + string.Empty + string.Empty + ... + "t" +  string.Empty + string.empty + "esting";

So actually you could place an endless array of string.empty between each character. 所以实际上你可以在每个字符之间放置一个无穷无尽的string.empty数组。

So I think 1 not possible 2 none, it just doens't make sense... 所以我认为1不可能2没有,它只是没有意义......

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

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