- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一些处理文本文件的 C# 代码,但我似乎无法让它与空白或空(空白)行正常工作。
我的代码:
while (!file.EndOfStream)
{
line = file.ReadLine();
bool isComment = (line[0] == '/') && (line[1] == '/');
bool isPoint = (line[0] == '(') && (line[line.Length - 1] == ')');
bool isWhiteSpace = string.IsNullOrEmpty(line);
Debug.Log("Comment: " + isComment + ", Point: " + isPoint + ", WhiteSpace: " + isWhiteSpace + "Value: '" + line + "'");
if (!isComment && !isPoint && !isWhiteSpace) { Application.Quit(); }
else if (isPoint)
{
//Strip parenthesis
line = line.Remove(line.Length - 1, 1).Remove(0, 1);
//break into float array
string[] arr = line.Split(',');
float xVal = float.Parse(arr[0]);
float yVal = float.Parse(arr[1]);
float zVal = float.Parse(arr[2]);
Vector3 currentVector = new Vector3(xVal, yVal, zVal);
results.Add(currentVector);
}
}
你可以看到我恰好在用 Vector3 做事。如果该行是注释行或空白行,我希望它什么也不做。如果它注意到括号,我希望它假设它是一个 Vector3 并解析它。最后,如果它不是这些线,我希望它完全停止。这是我使用记事本创建的示例文本文件:
//This is a comment
// ... and so is this!
(0, -1.5, 3)
(1, 4, 1.23)
(3, 5, 2)
请注意,第二个和第三个 Vector3 之间存在间隙。在这种特殊情况下,该行完全为空,不包含空格或任何内容,我只需在记事本中按 [Enter][Enter]。当我的脚本到达这一行时,它似乎触发了 file.EndOfStream boolean.... 但它不是文件的结尾!我怎样才能解决这个问题?我的 while 循环是否有更合适的条件?我还尝试读取该行并检查它是否为 null 作为 while 条件,这是一种更流行的处理方法,但这种处理方式也不适用于我的情况。
** 注意:“file”是StreamReader类型的变量**
最佳答案
这更像是一个风格说明而不是一个答案,尽管这也可以防止你看到的问题。
首先,使用 StreamReader
当您调用 ReadLine
时,您只会在到达文件末尾时收到 null
结果。您也不关心行首和行尾的空格,并且大概也不关心完全是空格的行。所以你可以用它来测试文件结尾和空行:
string line;
while ((line = file.ReadLine()) != null)
{
line = line.Trim();
if (line == "")
continue;
}
接下来,您将对开始/结束字符进行一些测试,这些字符在某些情况下仍会导致问题。具体来说,读取一行中只有一个字符的第二个字符会导致异常。
您可以使用 StartsWith
和 EndsWith
方法来进行测试,而不是对未测试长度的字符串使用索引:
bool isComment = line.StartsWith("//");
bool isPoint = line.StartsWith("(") && line.EndsWith(")");
最后,在解析点值的代码中,假设任何以 (
开头并以 )
结尾的行都至少有 2 个逗号,并且文本将正确解析。这是一个错误的假设。
处理所有这些问题的更好方法是随时检测和处理每种情况,将解析功能分解为可以重复使用的方法
这是我的版本:
public class Program
{
public static void Main()
{
List<Vector3> results = new List<Vector3>();
using (var file = System.IO.File.OpenText(@"C:\temp\test.txt"))
{
string line;
while ((line = file.ReadLine()?.Trim()) != null)
{
// skip empty lines and comments
if (line == string.Empty || line.StartsWith("//"))
continue;
// parse all other lines as vectors, exit program on error
try
{
Vector3 vector = ParseVector(line);
results.Add(vector);
}
catch (FormatException e)
{
Console.WriteLine("Parse error on line: {0}", line);
throw;
}
}
}
foreach (var v in results)
Console.WriteLine("({0},{1},{2})", v.X, v.Y, v.Z);
}
// parse string in format '(x,y,z)', all as floats
// throws FormatException on any error
public static Vector3 ParseVector(string text)
{
if (!text.StartsWith("(") || !text.EndsWith(")"))
throw new FormatException();
string[] parts = text.Substring(1, text.Length - 1).Split(',');
if (parts.Length != 3)
throw new FormatException();
float x = float.Parse(parts[0]);
float y = float.Parse(parts[1]);
float z = float.Parse(parts[2]);
return new Vector3(x, y, z);
}
}
如果您不想使用异常,您可以返回 null 或使用 TryParse
方法使用的模式,返回 bool 成功/失败指示符并使用 out
参数将结果写入。在这种情况下,我更喜欢异常(exception)。
关于c# - 如何在 C# 中正确处理空白、null 或空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33989127/
我正在寻找 css 属性以隐藏带或不带 css 类的段落,如果它包含空格 ( ) 或空白,但我想至少保留一个带或不带的段落,如果有更多的话。 隐藏段落,如果它是空白的或包含 white-space(
在 ruby 中对空白有不同的敏感度/设置吗? 我有一个 RoR 项目,其中一个事件记录调用有很多组件: max_stuff = FooSummary.select("max(stuff)
如何在脚注中的数字后留空? 一般来说,对于所有脚注! 例子: 好 : 1 Hello World 坏:1Hello World 最佳答案 正确答案是不要重新定义\thefootnote ,因为这会在脚
我有这段代码,每次第一个 for 循环再次开始时,我希望它将数组重置为空白,因为它正在使用新用户,但我得到的输出包含一个数组中的所有值。 var items = []; for (var i
我试图在CakePHP中生成一个动态xml文档,以输出到浏览器。 这是我的 Controller 代码: Configure::write ('debug', 0); $this->layout =
当我尝试在 nxos 设备上运行某些命令时,输出末尾有一个空格。我必须将输出与现有变量列表进行比较。末尾的空格导致比较错误。如何在字符串列表中使用 .strip() 函数? - name: Curre
我对 Elasticsearch 相当陌生,我一直在尝试对我的数据进行搜索,并且总是让点击部分为空。即使在数据上传和索引之后也会发生这种情况。我的映射如下: { "mappings":{
我想将about:blank页面更改为firefox插件首页页面的url。 如何更改默认的新标签页网址或可以为新标签页提供默认网址? 我正在使用Firefox附加SDK。 最佳答案 您可以结合使用Ta
我正在使用 R 并具有以下数据框示例,其中所有变量都是因子: first second third social birth control high
如何清空显示对话框的页面。下面是我的代码HTML: .ui-dialog, .ui-dialog-content { border:1px solid #cde68c; border-botto
更新“他的问题是要求我只运行一次 str ,他们已经告诉我该函数只需要一个参数)” 我试图返回第一个不重复的字符,例如:“blazqnqbla”->第一个不重复的字符是“z”,因此函数需要返回z。现在
我的登录验证有问题。问题是当我尝试使用管理员登录时,页面停止在 checklogin.php 上并且不会告诉它是否成功。这是我的代码。 索引.html Aplik
我的查询是这样的 SELECT Distinct tm.teamid,tm.Team_Name,CONCAT_WS(' ',tu.FirstName+' '+tu.LastName) as Leade
我正在创建指向页面的超链接 url 由用户输入决定,因此由查询字符串决定 ; 问题是变量状态由两个或多个单词组成。因此,当我尝试单击证明表单中输入的超链接时,仅获取状态变量的第一个单词。浏览器将另一个
该问题在每个浏览器中的表现都不同,例如在 Firefox 中大约一个空格如果您再次滚动到顶部,则会出现具有相同高度的滚动框。在 chrome 中,滚动时框会变得狭窄等等...... 使用的调用是:
我对菜单栏文字之间的 CSS 空白有疑问。我尝试了很多方法,但仍然无法解决。有人可以帮我吗? 菜单问题图片如下: http://imageshack.us/photo/my-images/201/44
我对 有疑问.其中的插入符根据是否为空具有不同的垂直位置: 我的代码: textarea { padding: 0 5px; border: none; outline: n
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Ignore whitespace in HTML 我想在网页上将图片并排放置。这是我的 HTML:
每当我尝试检查元素时,什么都没有出现。我在使用 Chrome。我明白了 Elements | Network | Sources | Timeline | Profiles | Resources |
我在使用 Chrome、Firefox 和 IE 时遇到了一个奇怪的问题。我正在为我的投资组合网站/博客构建一个 WordPress 主题,一切都很好,直到今天,当我在 chrome 中查看该网站时,
我是一名优秀的程序员,十分优秀!