c# - 'string' does not contain a definition for 'isNullorWhiteSpace' -
i'm not sure how incorporate own method code using isnullorwhitespace, framework isn't 4.0. i've had , suggested using isnullorwhitespace
, not preferred method display:
2/20/2014 7:33:10 am, measured velocity: 0.2225
i can't seem find equivalent code work.
using system; using system.collections.generic; using system.text; using system.io; using system.collections; namespace conapp { class program { public static class stringextensions { public static bool isnullorwhitespace(string value) { if (value != null) { (int = 0; < value.length; i++) { if (!char.iswhitespace(value[i])) { return false; } } } return true; } } static void main(string[] args) { string line; try { using (streamwriter sw = new streamwriter("c:\\writetest\\writetest.txt")) { string mydirpath = "c:\\chat\\"; string[] txtfilelist = directory.getfiles(mydirpath, "*.txt"); foreach (string txtname in txtfilelist) { system.io.streamreader sr = new system.io.streamreader(txtname); while ((line = sr.readline()) != null) { string spart = ".prt"; string sam = " am"; string spm = " pm"; string sresult = "test result: "; string svelocity = "measured velocity: "; string part = string.empty; string date = string.empty; string result = string.empty; string velocity = string.empty; // sw.writeline(line); if (line.contains(sam) || line.contains(spm)) { date = line; } if (line.contains(spart)) { part = line; } if (line.contains(sresult)) { result = line; } if (line.contains(svelocity)) { velocity = line; } if (!string.isnullorwhitespace(date) && !string.isnullorwhitespace(velocity)) { bool isnullorwhitespace = "foo bar".isnullorwhitespace(); //doesnt work here int = 2; string[] x = new string[i]; x[0] = date; x[1] = velocity; sw.writeline(x[0] + "," + x[1]); } } } } } catch { } } } }
firstly, stringextensions
needs top level class, cannot inside class.
secondly, need turn method extension method adding this
keyword first parameter:
public static bool isnullorwhitespace(this string value)
so becomes:
public static class stringextensions { public static bool isnullorwhitespace(this string value) { if (value != null) { (int = 0; < value.length; i++) { if (!char.iswhitespace(value[i])) { return false; } } } return true; } } class program { ... }
Comments
Post a Comment