-2

In java,

String.split(""); 

is possible. ex)

String[] str = "Hello world!".split("")

Like this, I want to split String without escape sequence. But in C#, I tried it and IDE said 'Error'. Is there any solution?

edit)
Code:

String[] str = "Hello world!".split("");

the result is str[0] = H, str[1] = e, ...(in java)

In C#, I tried

strI[i] = "Hello World!".Split('');

And the result is

'error CS1011: Empty character literal I want to split string with Empty literal.

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
Yeongsoo
  • 17
  • 3
  • Hi @Yeongsoo, could you please post the code you tried that gives you the error? – toti08 Sep 13 '18 at 06:12
  • What you expected from this? `"Hello world!".split("")` and what was wrong? – sujith karivelil Sep 13 '18 at 06:12
  • `I want to split String without escape sequence` what does this exactly mean – TheGeneral Sep 13 '18 at 06:17
  • 4
    You want every char? ToCharArray(). – Drag and Drop Sep 13 '18 at 06:19
  • 1
    From [javadoc](http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/StringUtils.html#split%28java.lang.String,%20char%29). `public static String[] split(String str)` _Splits the provided text into an array, using whitespace as the separator. Whitespace is defined by Character.isWhitespace(char)._ – Drag and Drop Sep 13 '18 at 06:27
  • 1
    It's the same in C# [Documentation](https://learn.microsoft.com/en-us/dotnet/api/system.string.split?view=netframework-4.7.2), We assume that default split char is space. `var result = "MyString".Split();` or `var input = "MyString here"; var result = input.Split();` – Drag and Drop Sep 13 '18 at 06:31

2 Answers2

0

The reason you are getting an error is because there is no method with the signature matching your call. In other words, when you call Split method, you must pass the first parameter as a character array or string array, whereas you are passing the first argument as simply a string.

Following call would not throw any error since it matches the signature of Split method.

string[] sArray = s.Split(new string[] {""}, StringSplitOptions.None);

However, above method will result in nothing productive since the resulting array will contain only one element whose value will be original string.

If your goal was to split a string into individual characters then you can use code like below.

string s = "some string";
var splitString = s.ToCharArray();
Sunil
  • 20,653
  • 28
  • 112
  • 197
0

Please note that no overloaded methods of String.Split() accepts a string as first argument like what you used. Following are the possible overloaded methods

enter image description here

The thing you are looking for is to split a string into characters. For that you can depend on String.ToCharArray Method, which will Copies the characters in this instance to a Unicode character array.:

char[] charArray = "Hello world!".ToCharArray();

So that we can access each characters using its index, that means - charArray[0] will be H.

If you need the splitted characters into a string array means you can use something like this:

string[] strArray = "Hello world!".Select(x=> x.ToString())
                                  .ToArray();
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88