Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does String.Split() not allow a single string as a parameter

Tags:

string

c#

list

I am trying to write one of my first c# scripts for a homeautomation solution (homeseer). I have other issues to resolve with the below code, however the simple line:

String[] parm = line.Split(",");

Results in the error:

Type 'string' does not contain a definition for `Split' and no extension method 'Split' of type 'string' could be found (are you missing a using directive or an assembly reference?)

I will post another question for my other issues

using System;
using System.Collections.Generic;

public void Main(string line)
{
    String[] parm = line.Split(",");
    var windowDoorOpenVar = hs.GetVar("WindowDoorOpen");
    if (windowDoorOpenVar.Size == 0 || windowDoorOpenVar == null)
    {
        hs.CreateVar("WindowDoorOpen");
        List<string> windowDoorOpen = new List<string>();
    }
    else
    {
        List windowDoorOpen = windowDoorOpenVar;
    }

    switch (parm[0])
    {
        case "Open":
            windowDoorOpen.Add(parm[1]);
            break;
        case "Closed":
            windowDoorOpen.Remove(parm[1]);
            break;
    }
    hs.SaveVar("WindowDoorOpen", windowDoorOpen);
}
like image 435
ACiD GRiM Avatar asked Apr 09 '26 17:04

ACiD GRiM


1 Answers

Split only has these overloads

Split(String[], Int32, StringSplitOptions)

Splits a string into a maximum number of substrings based on the strings in an array. You can specify whether the substrings include empty array elements.

Split(Char[], Int32, StringSplitOptions)

Splits a string into a maximum number of substrings based on the characters in an array.

Split(String[], StringSplitOptions)

Splits a string into substrings based on the strings in an array. You can specify whether the substrings include empty array elements

Split(Char[])

Splits a string into substrings that are based on the characters in an array.

Split(Char[], StringSplitOptions)

Splits a string into substrings based on the characters in an array. You can specify whether the substrings include empty array elements.

Split(Char[], Int32)

Splits a string into a maximum number of substrings based on the characters in an array. You also specify the maximum number of substrings to return.


If you really don't like typing the array new []{","} , you could roll-your-own Extension Method specifically for a single string and alike

public static class StringExtensions
{
   public static string[] Split(this string source, string value, StringSplitOptions options = StringSplitOptions.None)
   {
      return source?.Split(new[] { value }, options);
   }

   public static string[] Split(this string source, params string[] values)
   {
      return source?.Split(values, StringSplitOptions.None);
   }
}

...

// usage
var someString = "string";
someString.Split(",");
someString.Split(",",".");
someString.Split(",", StringSplitOptions.RemoveEmptyEntries);

Interesting engouh, the reason you can call Split(',') is because it uses a params array

params (C# Reference)

By using the params keyword, you can specify a method parameter that takes a variable number of arguments.

You can send a comma-separated list of arguments of the type specified in the parameter declaration or an array of arguments of the specified type. You also can send no arguments. If you send no arguments, the length of the params list is zero.

like image 110
TheGeneral Avatar answered Apr 11 '26 06:04

TheGeneral



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!