简体   繁体   English

无法遍历文档字符串

[英]unable to loop through documents strings

I feel I am missing something here and I was hoping someone can point me in the right direction, as I have been unable to find the answer. 我觉得我在这里遗漏了一些东西,我希望有人可以指出正确的方向,因为我一直找不到答案。

I have an application that look through a document and when a value is found it prompts the user to replace this value with something else.The issue I have is; 我有一个浏览文档的应用程序,当找到一个值时,它会提示用户将其替换为其他值。 say the document has 6 of the same value which we will use $ it will only update the first value and then move on. 假设文档中有6个值与我们将使用的$相同,它将仅更新第一个值,然后继续操作。

Document example: 文件范例:

    one $
    two  $
    three $
    four $
    five $

Now I understand that a string is immutable but I would have thought there is a way to loop through the information, My current code is as below; 现在我知道字符串是不可变的,但是我本以为有一种遍历信息的方法,我的当前代码如下;

DirectoryInfo di = new DirectoryInfo(scriptLocation);
FileInfo[] rgFiles = di.GetFiles("*.txt");
foreach (FileInfo fi in rgFiles)
{
    FileInfo fileInfo = new FileInfo(fi.FullName);
    string script = fileInfo.OpenText().ReadToEnd();
    int dollarIndex = script.IndexOf("$");
    string nextTenChars = script.Substring(dollarIndex - 17, 17);
    string promptValue = CreateInput.ShowDialog(nextTenChars, "Input");
    script = script.Replace("$", promptValue);
}

it will only update the first value and then move on. 它只会更新第一个值,然后继续。

The reason for that is that IndexOf returns the index of the first occurence in the string. 这样做的原因是IndexOf返回字符串中第一次出现的索引。 Since you have loaded the entire file as one string this manifests as a problem in your code. 由于您已将整个文件作为一个字符串加载,因此这在您的代码中表现为问题。

But if as you say: 但是,如果您说:

They will be 17 character in the proper documents and yeah the test will be line by line 在适当的文档中,它们将为17个字符,是的,测试将逐行进行

Then I would suggest to use System.IO.File.ReadAllLines to read the file. 然后,我建议使用System.IO.File.ReadAllLines读取文件。 It will return a string[] and at each position is a line. 它将返回一个string[]并且在每个位置都是一行。 So you can loop through this array and use your code 因此,您可以遍历此数组并使用您的代码

DirectoryInfo di = new DirectoryInfo(scriptLocation);
FileInfo[] rgFiles = di.GetFiles("*.txt");
foreach (FileInfo fi in rgFiles)
{
    string [] alllines = System.IO.File.ReadAllLines(fi.FullName);

    for (int i = 0; i < alllines.Length; i++)
    {
        if(alllines[i].Contains("$"))
        {
            // prompt
            int dollarIndex = alllines[i].IndexOf("$");
            string nextTenChars = alllines[i].Substring(dollarIndex - 17, 17);
            string promptValue = CreateInput.ShowDialog(nextTenChars, "Input");         
            alllines[i] = alllines[i].Replace("$", promptValue) ;
        }
    }
}

if the entire line consits only of the 17 chars plus the dollar sign you could simply drop the lines that handle this and proompt the user directly with the entire line for a replace 如果整行仅包含17个字符和美元符号,则可以简单地删除处理该行的行,并直接用整行修饰用户,以进行替换

To write the file back use simple the File.WriteAllLines method 要写回文件,请使用简单的File.WriteAllLines方法

String.IndexOf Method String.IndexOf方法

Reports the zero-based index of the first occurrence of a specified Unicode character or string within this instance. 报告此实例中首次出现指定的 Unicode字符或字符串的从零开始的索引。

So after first replace your loop is going to next file rather then looking further in the same file. 因此,在第一次替换之后,您的循环将转到下一个文件,而不是在同一文件中进一步查找。

You can use another loop till the end of document. 您可以使用另一个循环,直到文档末尾。

Using your code and a Split method: 使用您的代码和Split方法:

DirectoryInfo di = new DirectoryInfo(scriptLocation);
FileInfo[] rgFiles = di.GetFiles("*.txt");
foreach (FileInfo fi in rgFiles)
{
    FileInfo fileInfo = new FileInfo(fi.FullName);
    string script = fileInfo.OpenText().ReadToEnd();
    var values = script.Split('$');
    script = "";
    foreach (var value in values)
    {
        string promptValue = CreateInput.ShowDialog(value, "Input");
        script += promptValue;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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