-1

i have that VB code...

Dim htcsplit() As String = Split(value.Text.Replace(" ", ""), ",")

try to adapt this for c#.

Unluckily i cant write

string[] htcsplit = String.Split(value.Text.Replace(' ', null),',');

Because the literal cant be empty.

Is there a workaround ?

Glowhollow
  • 132
  • 3
  • 13
  • It's the replace function - take a look at https://stackoverflow.com/questions/6373315/how-to-replace-a-char-in-string-with-an-empty-character-in-c-net – Allan S. Hansen Apr 05 '19 at 09:53
  • You might find Convert .NET a useful tool. It's a desktop tool with integrated RegEx-Tester and VB.Net<=>C# translation: https://fishcodelib.com/Convert.htm among other functions. – LocEngineer Apr 05 '19 at 10:03
  • Is there a reason you changed from the *string* `" "` to the *char* `' '`? – Andrew Morton Apr 05 '19 at 10:08

3 Answers3

1

This should work:

string[] htcsplit = value.Text.Replace(" ", "").Split(',');
ThePerplexedOne
  • 2,920
  • 15
  • 30
0

The equivalent in C# should be:

string[] htcsplit = value.Text.Replace(" ", string.Empty).Split(',');

or (if you have a Split method)

string[] htcsplit = Split(value.Text.Replace(" ", string.Empty), ',');

Example Split method:

string[] Split(string input, char separator)
{
    return input.Split(separator);
}

There are some converters out there which could help you:

kapsiR
  • 2,720
  • 28
  • 36
0

Instead of "" you can use string.Empty This replaces the value as string instead of a character, but should bring the same result. The string with the replaced characters can be split by a char or string.

Try:

string[] htcsplit = value.Text.Replace(" ", string.Empty).Split(',');

or

string[] htcsplit = value.Text.Replace(" ", string.Empty).Split(",");
  • There is no difference between "" and `string.Empty`. As I see it, using `string.Empty` is recommended for readability and avoiding unnecessary compiler generated constants, but in the latter case I assume that the compiler nowadays is smart enough to optimize even multiple "" literals. – Bart Hofland Apr 05 '19 at 17:20
  • Yep, "" equals string.Empty. I wanted to point out, that the replace function is now based on the string datatype instead of char, as the OP had originally tried to write. And "" looks always a little bit tinkered to me. ;o) –  Apr 05 '19 at 17:33