简体   繁体   English

如何从我一直在寻找索引的位置继续?

[英]How to continue from where I have been searching to find the index?

How to continue from where I have been searching to find the index? 如何从我一直在寻找索引的位置继续?

I am searching in a file to find the index of a character; 我在文件中搜索以查找字符的索引; then I have to continue from there to find the index of the next character. 然后我必须从那里继续查找下一个字符的索引。 For example : string is " habcdefghij" 例如:字符串是“ habcdefghij”

       int index = message.IndexOf("c");
        Label2.Text = index.ToString();
        label1.Text = message.Substring(index);
        int indexend = message.IndexOf("h");
        int indexdiff = indexend - index;
       Label3.Text = message.Substring(index,indexdiff);

so it should return "cedef" 所以它应该返回“ cedef”

but the second search starts from the beginning of the file, it will return the index of first h rather than second h:-( 但是第二次搜索从文件开头开始,它将返回前一个h的索引,而不是第二个h :-(

You can specify a start index when using String.IndexOf. 使用String.IndexOf时可以指定起始索引。 Try 尝试

//...
int indexend = message.IndexOf("h", index); 
//...
int index = message.IndexOf("c");
label1.Text = message.Substring(index);

int indexend = message.IndexOf("h", index); //change

int indexdiff = indexend - index;
Label3.Text = message.Substring(index, indexdiff);

This code finds all the matches, and shows them in order: 此代码查找所有匹配项,并按顺序显示它们:

 // Find the full path of our document
        System.IO.FileInfo ExecutableFileInfo = new System.IO.FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location);            
        string path = System.IO.Path.Combine(ExecutableFileInfo.DirectoryName, "MyTextFile.txt");

    // Read the content of the file
    string content = String.Empty;
    using (StreamReader reader = new StreamReader(path))
    {
        content = reader.ReadToEnd();
    }

    // Find the pattern "abc"
    int index = content.Length - 1;

    System.Collections.ArrayList coincidences = new System.Collections.ArrayList();

    while(content.Substring(0, index).Contains("abc"))
    {
        index = content.Substring(0, index).LastIndexOf("abc");
        if ((index >= 0) && (index < content.Length - 4))
        {
            coincidences.Add("Found coincidence in position " + index.ToString() + ": " + content.Substring(index + 3, 2));                    
        }
    }

    coincidences.Reverse();

    foreach (string message in coincidences)
    {
        Console.WriteLine(message);
    }

    Console.ReadLine();

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

相关问题 如何找到应该调用静态方法的位置? - How can I find where a static method should have been called? C# 如何转换从数据库中检索到的日期以及我应该在哪里写? - C# How to convert the date that have been retrieve from DB and Where should I write? 搜索文件时如何从 c# 中的 UnauthorizedAccessException 错误继续 - How to continue from a UnauthorizedAccessException error in c# when searching for files 如何从停止位置继续为列表编号? - How to continue numbering a list from where it stops? 我怎样才能让播放器从我暂停的地方继续漂浮? - How can I make the player continue floating from the spot where I paused it from? 如何确定是否已在 SQLite 表上创建索引? - How can I find out if an index has been created on a SQLite table? 我一直在寻找解决方案,但是在上传图片时,我的更新查询中仍然有错误 - I have been searching solution for this but still having error in my update query at the time of image upload 如何让程序知道在哪里启动? - How to have a program know where it has been launched? 如何跟踪我从哪里泄漏IDisposable对象? - How do I track down where I've been leaking IDisposable objects from? 是否可以找出从 1 个类型派生的类型 - Is it possible to find out the types that have been derived from 1 type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM