简体   繁体   中英

System.Array does not contain a definition for 'Split' And no extension method "Split"

I've seen the previous post that an array can't be split, but have no clue what to do in order to bypass the error.

private DataTable SplitStringInto(string fldName, string txt, params string[] splitters) {
        DataTable dt = new DataTable();
        dt.Columns.Add(fldName, typeof(string));
        foreach (string s in splitters.Split(StringSplitOptions.None)) { //error appear here
            dt.Rows.Add(new object[] {
                        s});
        }
        
        return dt;
    }

Am trying to move this Vb code to C#

You, probably, want to split text - txt with splitters provided (say, "," , ";" , "\\t" ):

private DataTable SplitStringInto(string fldName, string txt, params string[] splitters) {
  DataTable dt = new DataTable();
  dt.Columns.Add(fldName, typeof(string));

  foreach (string s in txt.Split(splitters, StringSplitOptions.None)) 
    dt.Rows.Add(new object[] { s });

  return dt;
}

And you call it like this:

DataTable result = SplitStringInto("myField", "line1;line2,line3", ";", ",", "\t");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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