字符串匹配

时间:2010-04-08 10:31:59

标签: c# .net winforms string

我有一个字符串

String mainString="///BUY/SELL///ORDERTIME///RT///QTY///BROKERAGE///NETRATE///AMOUNTRS///RATE///SCNM///";

现在我有另一个字符串

String str1= "RT";

应仅与RT匹配,mainString是字符串ORDERTIME的子字符串,但不与mainString匹配, String str2= "RATE" ; 也是字符串RATE的子字符串。

RATE

并且mainString(str2)应与NETRATE匹配,mainString是字符串{{1}}的子字符串,但不与{{1}}匹配,{{1}}也是字符串{{1}}的子字符串

我们怎么能这样做?

5 个答案:

答案 0 :(得分:2)

"///RT///""///RATE///"匹配。

答案 1 :(得分:0)

据我所知,您希望将///之间的字符串作为分隔符进行匹配 如果你寻找str,你只需要做  Regex.Match(mainString, "(^|///)" + str + "(///|$)");

答案 2 :(得分:0)

这可能会给你一些线索 - 没有接近真正的代码质量,只有5分钟的工作来提供这个劣质的解决方案,但确实做你需要的。闻起来很有气味。

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace test {
    class Program {
        static void Main(string[] args) {
            String mainString="//BUY/SELL//ORDERTIME//RT//QTY//BROKERAGE//NETRATE//AMOUNTRS//RATE//SCNM//";


            Hashtable ht = createHashTable(mainString);



            if (hasValue("RA", ht)) {
                Console.WriteLine("Matched RA");
            } else {
                Console.WriteLine("Didnt Find RA");
            }


            if (hasValue("RATE", ht)) {
                Console.WriteLine("Matched RATE");
            }


            Console.Read();

        }


        public static Hashtable createHashTable(string strToSplit) {
            Hashtable ht = new Hashtable();
            int iCount = 0;

            string[] words = strToSplit.Split(new Char[] { '/', '/', '/' });
            foreach (string word in words) {

                ht.Add(iCount++, word);
            }

            return ht;
        }
        public static bool hasValue(string strValuetoSearch, Hashtable ht) {

            return ht.ContainsValue(strValuetoSearch);

        }

    }

}

答案 3 :(得分:0)

我不知道它每次都会起作用。但是我已经尝试了这个,它现在可以在这个字符串匹配中使用。我想知道这是否合适,请给我一些建议。

str1 = str1.Insert(0, "///");
str1=str1.Insert(str1.Length,"///");

bool Result = mainString.Contains(str1);

答案 4 :(得分:0)

Linq to Object怎么样?

String mainString="///BUY/SELL///ORDERTIME///RT///QTY///BROKERAGE///NETRATE///AMOUNTRS///RATE///SCNM///";
String searchTerm = "RT";
String[] src = mainString.split('///');
var match = from word in src where 
            word.ToLowerInvariant() == searchTerm.ToLowerInvariant() 
            select word;

我没有VS靠近我所以我无法测试它,但它应该是类似于此。