String.Empty in strings

2 days ago, a question arose related to string.LastIndexOf(String.Empty) returning the last row index:

C # lines end with an empty string?

So I thought; a string can always contain string.empty between characters of type:

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

After that, I wanted to check if String.IndexOf(String.Empty) returning 0, because since String.Empty can be between any char in the string, this will be what I expect it to return, and I was not mistaken.

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

In fact, he returned 0. I started thinking that if I could split the string with String.Empty , I would get at least 2 strings, and they would be String.Empty and the rest of the string, since String.IndexOf(String.Empty) returned 0 and string.LastIndexOf(String.Empty) returns the length of the string. Here is what I encoded:

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

The problem here is that I cannot obviously convert String.Empty to char[] and result in an empty string []. I would really like to see the result of this operation and the reason for this. So my questions are:

  • Is there a way to split a string by String.Empty ?

  • If this is not possible, but in an absolute world, that would be possible if it would return an array full of characters such as [0] = "t" [1] = "e" [2] = "s" , etc. , or just return the full string? What will make more sense and why?

+6
string split c # string-comparison
source share
6 answers

Do you really need to split the string, or are you just trying to get all the individual characters?

If so, then the string is also an IEnumerable<char> , and you also have an index.

So what are you really trying to do?

And no, you cannot call split methods with string.Empty or similar constructs.

+1
source share

You will always get index 0 when you search for String.Empty on any string, because this is the definition of String.IndexOf(String.Empty) , you should look at MSDN , which says:

"The index position is based on the zero value if this string is found, or -1 if it is not. If the value is String.Empty, the return value is 0.

Directed to the second question:

I think you can split the string with an empty string by doing something like this in your code:

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

BTW: Possible Clone of this answer Why does "abcd" .StartsWith (") return true?

+2
source share

Yes, you can split any line with .Empty line

  string[] strArr = s.Split(string.Empty.ToCharArray()); 
+2
source share
 string emptyString = string.Empty; char[] emptyStringCharArr = emptyString.ToCharArray(); 

This will give you an empty array of characters. This is because String is already an array of characters in memory, and String.Empty does not matter.

To break it down further, consider implementing .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; } 

The length of the course will be zero, and you will return an empty char array. Of course, this is not an exact implementation, but you can see how and why it does not return anything (and therefore does not break into a string, as you expect)

It is not an array with a single String.Empty element, because it does not make sense. When you try to split into an empty array, it does not know how and why you need to split, so the original string is returned to you.

As for why it returns 0 by default, consider:

 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 passed (even String.Empty). For this reason, itโ€™s much faster to add a check and return 0 independently (just as if it were executing a loop).

+1
source share

Since String.Empty is just an empty string, so if you do:

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

this will result in exactly the same line as:

 var s = "part1" + "part2"; 

the first syntax will not insert a magic blank line between two parts.

The fact that IndexOf returns 0, by definition, is not because there is some magic empty string between the characters.

I cannot imagine a logical way to split a string into an empty string. What should he return? If you use an empty string as an argument to the string.Split method, it will be ignored. If it was a single delimiter, the string will be returned unsplit.

0
source share

you can also say

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

So in fact you can place an infinite string.empty array between each character.

So, I think 1 is impossible 2 no, it just doesn't make sense ...

0
source share

All Articles