问题描述
我不知道我会如何使用isNullorWhiteSpace结合我自己的方法转换成代码,我的框架并不4.0。我有一些帮助,以前和他们建议使用 isnullorwhitespace
,是不是要显示的最优选的方法:
我可以'不像是会找到等效代码,将工作。
使用系统;
使用System.Collections.Generic;
使用System.Text;
:使用System.IO; System.Collections中使用
;
命名空间CONAPP
{
类节目
{
公共静态类StringExtensions
{
公共静态布尔IsNullOrWhiteSpace(字符串值!)
{
如果(值= NULL)
{
的for(int i = 0; I< value.Length;我++)
{
如果
{
返回假(char.IsWhiteSpace(值[I])!);
}
}
}
返回真;
}
}
静态无效的主要(字串[] args)
{
串线;
试
{使用
(StreamWriter的SW =新的StreamWriter(C:\\writetest\\writetest.txt))
{
串mydirpath =C:\\chat\\
的String [] = txtFileList Directory.GetFiles(mydirpath,的* .txt);
的foreach(在txtFileList串txtName的)
{
就是System.IO.StreamReader SR =新就是System.IO.StreamReader(txtName的);
,而((行= sr.ReadLine())!= NULL)
{
字符串SPART =.PRT;
字符串SAM =AM;
字符串SPM =PM;
字符串sresult =测试结果:;
字符串SVELOCITY =测量的速度:
字符串的一部分=的String.Empty;
字符串日期=的String.Empty;
字符串结果=的String.Empty;
字符串速度=的String.Empty;
// sw.WriteLine(线);
如果(line.Contains(SAM)|| line.Contains(SPM))
{
日期=行;
}
如果(line.Contains(SPART))
{
部分=行;
}
如果(line.Contains(sresult))
{
结果=线;
}
如果(line.Contains(SVELOCITY))
{
速度=行;
}
如果(String.IsNullOrWhiteSpace(日期)及!&安培;!String.IsNullOrWhiteSpace(速度))
{
布尔isNullOrWhiteSpace =富巴 .IsNullOrWhiteSpace(); //这里行不通
INT I = 2;
的String [] X =新的字符串[I]
X [0] =日期;
X [1] =速度;
sw.WriteLine(X [0] +,+ X [1]);
}
}
}
}
}
抓
{
}
}
}
}
首先, StringExtensions
需要一个顶层类,所以它不能被另一个类中。结果
其次,你需要加入这个
关键字来第一个参数开启方法为扩展方法:
公共静态布尔IsNullOrWhiteSpace(此字符串值)
所以就变成:
公共静态类StringExtensions
{
公共静态布尔IsNullOrWhiteSpace( !此字符串值)
{
如果(值= NULL){
的for(int i = 0; I< value.Length;我++){
如果(字符! .IsWhiteSpace(值[I])){
返回FALSE;
}
}
}
返回真;
}
}
类节目
{
:
}
I'm not sure how I could incorporate my own method into code using isNullorWhiteSpace, my framework isn't 4.0. I've had some help previously and they suggested using isnullorwhitespace
, is it not the most preferred method to display:
I can't seem to find equivalent code that will 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)
{
for (int i = 0; i < 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 I = 2;
string[] x = new string[I];
x[0] = date;
x[1] = velocity;
sw.WriteLine(x[0] + "," + x[1]);
}
}
}
}
}
catch
{
}
}
}
}
Firstly, StringExtensions
needs to be a top level class, so it cannot be inside another class.
Secondly, you need to turn the method into an extension method by adding the this
keyword to the first parameter:
public static bool IsNullOrWhiteSpace(this string value)
So it becomes:
public static class StringExtensions
{
public static bool IsNullOrWhiteSpace(this string value)
{
if (value != null) {
for (int i = 0; i < value.Length; i++) {
if (!char.IsWhiteSpace(value[i])) {
return false;
}
}
}
return true;
}
}
class Program
{
...
}
这篇关于“串”不包含“isNullorWhiteSpace”的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!