在字符串中搜索字符串

时间:2014-12-03 10:33:02

标签: c# .net search

亲爱的stackoverflow成员,

我有这个字符串:

 string data = "1Position1234Article4321Quantity2Position4323Article3323Quantity";

我想搜索“关键字”为“位置”的值。在这种情况下,我想要返回1和2.每个值都使用自己的“关键字”进行“索引”。因此,此字符串中的值1具有“位置”分隔符。值1234具有Article分隔符,值4321具有Quantity分隔符。

我需要一种方法来搜索字符串,并希望获得所有位置,文章和数量。没有关键字。

输出喊叫是:

 string[] position = {"1", "2"};
 string[] article = {"1234", "4323"};
 string[] quantity = {"4321", "3323"};

希望有些人可以帮助我。

谢谢!

4 个答案:

答案 0 :(得分:1)

这是我在LinqPad中提出的快速解决方案:

void Main()
{
        string data = "1Position1234Article4321Quantity2Position4323Article3323Quantity";
        var Articles = Indices(data, "Article").Dump("Articles: ");
        var Posistions = Indices(data, "Position").Dump("Positions :");
        var Quantities = Indices(data, "Quantity").Dump("Quantities :");
}

// Define other methods and classes here
public List<int> Indices(string source, string keyword)
{
    var results = new List<int>();
    //source: http://stackoverflow.com/questions/3720012/regular-expression-to-split-string-and-number
    var temp = Regex.Split(source, "(?<Alpha>[a-zA-Z]*)(?<Numeric>[0-9]*)").ToList().Where (r => !String.IsNullOrEmpty(r)).ToList();
    //select the list with index only where key word matches
    var indices = temp.Select ((v,i) => new {index = i, value = v})
                      .Where (t => t.value == keyword);
    foreach (var element in indices)
    {
        int val;
        //get previous list entry based on index and parse it
        if(Int32.TryParse(temp[element.index -1], out val))
        {
            results.Add(val);
        }
    }
    return results;
}

输出:enter image description here

答案 1 :(得分:0)

这是一种可能的算法:

  • 通过列表运行并获取每个数字/关键字。
  • 将它们放入带有“关键字”键的字典中,将所有“数字”列为一个列表。
  • 迭代字典并打印它们的键+值。

答案 2 :(得分:0)

下面的代码段可以用来获得与预期相符的输出。

string data = "1Position1234Article4321Quantity2Position4323Article3323Quantity";

StringBuilder sb = new StringBuilder();
StringBuilder sbWord = new StringBuilder();
bool isDigit = false;
bool isChar = false;
Dictionary<int, string> dic = new Dictionary<int, string>();
int index = 0;

for (int i = 0; i < data.Length; i++)
 {
  if (char.IsNumber(data[i]))
   {
     isDigit = true;
     if (isChar)
      {
         dic.Add(index, sb.ToString() + "|" + sbWord.ToString());
         index++;
         isChar = false;
         sb.Remove(0, sb.Length);
         sbWord.Remove(0, sbWord.Length);
      }

     }
     else
            {
                isDigit = false;
                isChar = true;
                sbWord.Append(data[i]);
            }

            if (isDigit)
                sb.Append(data[i]);

            if (i == data.Length - 1)
            {
                dic.Add(index, sb.ToString() + "|" + sbWord.ToString());
            }
        }

        List<string> Position = new List<string>();
        List<string> Article = new List<string>();
        List<string> Quantity = new List<string>();

        if (dic.Count > 0)
        {
            for (int i = 0; i < dic.Count; i++)
            {
                if (dic[i].Split('|')[1] == "Position")
                    Position.Add(dic[i].Split('|')[0]);


                else if (dic[i].Split('|')[1] == "Article")

                    Article.Add(dic[i].Split('|')[0]);

                else
                    Quantity.Add(dic[i].Split('|')[0]);
            }
        }

        string[] Position_array = Position.ToArray();
        string[] Article_array = Article.ToArray();
        string[] Quantity_array = Quantity.ToArray();

答案 3 :(得分:-1)

尝试这个简单的解决方案。

class StrSplit{
  public static void main(String args[]){

   int i;
  String str = "1Position1234Article4321Quantity2Position4323Article3323Quantity";
  String pattern=  "(?<=Position)|(?<=Article)|(?<=Quantity)";

  String[] parts = str.split(pattern);
  List<String> Position = new ArrayList<String>();
  List<String> Article = new ArrayList<String>();
  List<String> Quantity = new ArrayList<String>();

     for( i=0;i<parts.length;i++)
     {
          pattern="Position";
          String[] subParts;
          if(parts[i].contains(pattern))
          {
                 subParts = parts[i].split(pattern);
                 Position.add(subParts[0]);
          }
          pattern="Article";
          if(parts[i].contains(pattern))
          {
                   subParts = parts[i].split(pattern);
                   Article.add(subParts[0]);
          }
          pattern="Quantity";
          if(parts[i].contains(pattern))
          {
                  subParts = parts[i].split(pattern);
                  Quantity.add(subParts[0]);
          }

   }
   System.out.println("Position:");
   for(i = 0; i < Position.size(); i++) {
        System.out.println(Position.get(i));
    }
     System.out.println("Article:");
   for(i = 0; i < Article.size(); i++) {
        System.out.println(Article.get(i));
    }
     System.out.println("Quantity:");
 for(i = 0; i < Quantity.size(); i++) {
        System.out.println(Quantity.get(i));
    }
}
}

<强>输出:
Position: 1 2 Article: 1234 4323 Quantity: 4321 3323