在C#中查找一个较大字符串中所有子字符串的位置

94

我有一个大字符串需要解析,我需要找到所有extract"(me,i-have lots. of]punctuation的实例,并将每个实例的索引存储到一个列表中。

假设这个字符串片段在较大的字符串的开头和中间出现,两个实例都会被找到,并将它们的索引添加到List中。该List将包含0和另一个索引,无论它是什么。

我已经尝试过使用string.IndexOf方法,它几乎可以完成我想要的功能,但我编写的代码并不起作用,而且我一直无法确定问题所在:

List<int> inst = new List<int>();
int index = 0;
while (index < source.LastIndexOf("extract\"(me,i-have lots. of]punctuation", 0) + 39)
{
    int src = source.IndexOf("extract\"(me,i-have lots. of]punctuation", index);
    inst.Add(src);
    index = src + 40;
}
  • inst = 列表
  • source = 大字符串

还有更好的想法吗?

16个回答

0
public static Dictionary<string, IEnumerable<int>> GetWordsPositions(this string input, string[] Susbtrings)
{
    Dictionary<string, IEnumerable<int>> WordsPositions = new Dictionary<string, IEnumerable<int>>();
    IEnumerable<int> IndexOfAll = null;
    foreach (string st in Susbtrings)
    {
        IndexOfAll = Regex.Matches(input, st).Cast<Match>().Select(m => m.Index);
        WordsPositions.Add(st, IndexOfAll);

    }
    return WordsPositions;
}

0

我知道这是旧的,但我想,我会将列表答案转换为整数数组答案(没有在这里看到过)。

    int[] GetPositionsAsArray(string sourceString, string searchString)
    {
        if (String.IsNullOrEmpty(searchString) || String.IsNullOrEmpty(sourceString))
        {
            return new int[] { -1 };
        }
        else
        {    
            int[] ret = new int[sourceString.Length];
            int len = searchString.Length;               
            int index = 0;
            int start = -len;
            while (true)
            {
                start = sourceString.IndexOf(searchString, start + len);
                if (start == -1)
                {
                    break;
                }
                else
                {
                    ret[index] = start;
                    index++;
                }
            }
            Array.Resize(ref ret, index); //optional
            return ret;
        }
    }

-1
这个替代实现怎么样?
 public static class MyExtensions
    {
        public static int HowMany(this string str, char needle)
        {
            int counter = 0;
            int nextIndex = 0;
            for (; nextIndex != -1; )
            {
                nextIndex = str.IndexOf(needle, nextIndex);
                if (nextIndex != -1)
                {
                    counter++;
                    //step over to the next char
                    nextIndex++;
                }
            }
            return counter;
        }
    }

请不要在答案中提问,而是用解释来回答问题。在这种情况下,请解释为什么您的答案比其他给出的答案更好。 - Gert Arnold

-1

你可以使用LINQ来选择和枚举所有元素,然后通过任何字符串查找:

我已经创建了一个类:

class Pontos 
{
    //index on string
    public int Pos { get; set; }
    //caractere 
    public string Caractere { get; set; }           
}

使用方法如下:

int count = 0;

var pontos = texto.Select(y => new Pontos { Pos = count++, Caractere = y.ToString() }).Where(x=>x.Caractere == ".").ToList();

然后: 输入字符串: enter image description here

输出列表: enter image description here

附注: SeForNumero 是我类的另一个字段,我需要它来完成自己的目的,但对于这种使用不是必需的。


-1

我发现了这个示例,并将其合并到一个函数中:

    public static int solution1(int A, int B)
    {
        // Check if A and B are in [0...999,999,999]
        if ( (A >= 0 && A <= 999999999) && (B >= 0 && B <= 999999999))
        {
            if (A == 0 && B == 0)
            {
                return 0;
            }
            // Make sure A < B
            if (A < B)
            {                    
                // Convert A and B to strings
                string a = A.ToString();
                string b = B.ToString();
                int index = 0;

                // See if A is a substring of B
                if (b.Contains(a))
                {
                    // Find index where A is
                    if (b.IndexOf(a) != -1)
                    {                            
                        while ((index = b.IndexOf(a, index)) != -1)
                        {
                            Console.WriteLine(A + " found at position " + index);
                            index++;
                        }
                        Console.ReadLine();
                        return b.IndexOf(a);
                    }
                    else
                        return -1;
                }
                else
                {
                    Console.WriteLine(A + " is not in " + B + ".");
                    Console.ReadLine();

                    return -1;
                }
            }
            else
            {
                Console.WriteLine(A + " must be less than " + B + ".");
               // Console.ReadLine();

                return -1;
            }                
        }
        else
        {
            Console.WriteLine("A or B is out of range.");
            //Console.ReadLine();

            return -1;
        }
    }

    static void Main(string[] args)
    {
        int A = 53, B = 1953786;
        int C = 78, D = 195378678;
        int E = 57, F = 153786;

        solution1(A, B);
        solution1(C, D);
        solution1(E, F);

        Console.WriteLine();
    }

返回:

在位置2找到53

在位置4找到78
在位置7找到78

57不在153786中


1
嗨,马克,我看到你是stackoverflow的新手。这个答案对于这个老问题没有任何帮助,已经有更好的答案了。如果将来回答这样的问题,请尝试解释为什么你的答案包含一些其他答案中不存在的信息或价值。 - caesay

-1

基于我用于在较大字符串中查找多个字符串实例的代码,您的代码将如下所示:

List<int> inst = new List<int>();
int index = 0;
while (index >=0)
{
    index = source.IndexOf("extract\"(me,i-have lots. of]punctuation", index);
    inst.Add(index);
    index++;
}

这里有两个问题:首先,你总是向结果列表中添加-1,这不是一个有效的结果。其次,由于indexOf返回-1和index++,代码无法终止。如果IndexOf的结果为-1,我会使用while(true)break; - b-pos465

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接