C# 3.0 Cookbook:一、字符与字符串处理(3):在一个字符串内寻找另一个字符串出现的所有位置

问题:

    在一个字符串中搜索另一个特定字符串出现的位置,另外,在搜索时控制大小写敏感。

解决方案:

    在一个循环中使用IndexOf或IndexOfAny方法,可得到一个字符在另一个字符串中出现的次数以及存在的位置,使用如下代码可以实现使用大小写敏感的搜索一个字符串在另一个字符串中出现的次数。

using System;
using System.Collections;
using System.Collections.Generic;
static class CharStrExtMethods
{
    public static int[] FindAll(this string matchStr, string searchedStr, int startPos)
    {
        int foundPos = -1; // -1 represents not found.
        int count = 0;
        List<int> foundItems = new List<int>();
        do
        {
            foundPos = searchedStr.IndexOf(matchStr, startPos, StringComparison.Ordinal);
            if (foundPos > -1)
            {
                startPos = foundPos + 1;
                count++;
                foundItems.Add(foundPos);
                Console.WriteLine("Found item at position: " + foundPos.ToString());
            }
        } while (foundPos > -1 && startPos < searchedStr.Length);
        return ((int[])foundItems.ToArray());
    }
}

    假设FindAll扩展方法在调用时使用以下参数。

string data = "Red";
int[] allOccurrences = data.FindAll("BlueTealRedredGreenRedYellow", 0);

    字符串“Red”在字符串searchedStr中出现的位置索引分别为8和19。这段代码在循环中使用IndexOf方法反复在searchStr中搜索与matchStr匹配的项。

    在一个字符串中使用大小写敏感搜索一个字符,可以使用以下代码:

static class CharStrExtMethods
{
    public static int[] FindAll(this char MatchChar, string searchedStr,
    int startPos)
    {
        int foundPos = -1; // -1 represents not found.
        int count = 0;
        List<int> foundItems = new List<int>();
        do
        {
            foundPos = searchedStr.IndexOf(MatchChar, startPos);
            if (foundPos > -1)
            {
                startPos = foundPos + 1;
                count++;
                foundItems.Add(foundPos);
                Console.WriteLine("Found item at position: " + foundPos.ToString());
            }
        } while (foundPos > -1 && startPos < searchedStr.Length);
        return ((int[])foundItems.ToArray());
    }
}

    假设FindAll扩展方法在调用时使用以下参数。

char data = "r";
int[] allOccurrences = data.FindAll("BlueTealRedredGreenRedYellow", 0);

    字母“r”在searchedStr中出现的位置索引分别为11和15。这段代码在循环中使用IndexOf扩展方法反复在searchStr中搜索与matchStr匹配的项。重载FindAll方法接收任意一个char或string类型,通过由传入的char类型对象创建一个新的string类型对象以避免执行时的产生冲突。

    在一个字符串中不使用大小写敏感搜索每一个字符串出现的次数,可以使用以下代码:

static class CharStrExtMethods
{
    public static int[] FindAny(this string matchStr, string searchedStr, int
    startPos)
    {
        int foundPos = -1; // -1 represents not found.
        int count = 0;
        List<int> foundItems = new List<int>();
        // Factor out case-sensitivity
        searchedStr = searchedStr.ToUpperInvariant();
        matchStr = matchStr.ToUpperInvariant();
        do
        {
            foundPos = searchedStr.IndexOf(matchStr, startPos,
            StringComparison.Ordinal);
            if (foundPos > -1)
            {
                startPos = foundPos + 1;
                count++;
                foundItems.Add(foundPos);
                Console.WriteLine("Found item at position: " + foundPos.ToString());
            }
        } while (foundPos > -1 && startPos < searchedStr.Length);
        return ((int[])foundItems.ToArray());
    }
}

    假设FindAny扩展方法在调用时使用以下参数。

string data = "Red";
int[] allOccurrences = data.FindAny("BlueTealRedredGreenRedYellow",0);

    字符串“Red”在字符串searchedStr中出现的位置索引分别为8,11和19。这段代码在循环中使用IndexOf方法反复在searchStr中搜索与matchStr匹配的项。使用ToUpperInvariant方法传递大小写不敏感的searchedStr字符串和matchStr字符串进行搜索。

    在一个字符串中查询某个字符的集合,可以使用以下代码。

static class CharStrExtMethods
{
    public static int[] FindAny(this char[] MatchCharArray, string
    searchedStr,
    int startPos)
    {
        int foundPos = -1; // -1 represents not found.
        int count = 0;
        List<int> foundItems = new List<int>();
        do
        {
            foundPos = searchedStr.IndexOfAny(MatchCharArray, startPos,
                       StringComparison.Ordinal);
            if (foundPos > -1)
            {
                startPos = foundPos + 1;
                count++;
                foundItems.Add(foundPos);
                Console.WriteLine("Found item at position: " + foundPos.
                ToString());
            }
        } while (foundPos > -1 && startPos < searchedStr.Length);
        return ((int[])foundItems.ToArray());
    }
}

    假设FindAll扩展方法在调用时使用以下参数。

char[] data = new char[] {'R', 'r'};
int[] allOccurrences = data.FindAny("BlueTealRedredGreenRedYellow",0);

    字符“r”和“R”在字符串searchedStr中搜索到的位置索引分别为8,11,15和19。这段代码在循环中使用IndexOf方法反复在searchStr中搜索与matchStr匹配的项。依靠传递一个大小写不敏感的包含所有大小写字符的char型数组进行搜索。

讨论:

    在示例代码中,变量foundPos包含了

版权说明:作者:张颖希PocketZ's Blog
出处:http://www.cnblogs.com/PocketZ
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

本文翻译内容取自网络,纯粹是练习英文水平,如有雷同,纯属意外!有不妥之处,欢迎拍砖!

版权说明:作者:张颖希PocketZ's Blog
出处:http://www.cnblogs.com/PocketZ
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
若本文为翻译内容,目的为练习英文水平,如有雷同,纯属意外!有不妥之处,欢迎拍砖

原文地址:https://www.cnblogs.com/PocketZ/p/1714371.html