String Matching

I have a string

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

Now I have another strings

String str1= "RT";

which should be matched only with RT which is substring of string mainString but not with ORDERTIME which is also substring of string mainString.

 String str2= "RATE" ;

And RATE(str2) should be matched with RATE which is substring of string mainString but not with NETRATE which is also substring of string mainString.

How can we do that ?

Answers


Match against "///RT///" and "///RATE///".


This might give you some clues - no where near real code quality, and only a 5 minute job to come with this shoddy solution but does do what you need. it smells lots be warned.

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);

        }

    }

}

as far as I understand your question you want to match a string between /// as delimiters. if you look for str you just have to do Regex.Match(mainString, "(^|///)" + str + "(///|$)");


I don't know it will work every time or not.But I have tried this and it works right now in this string matching. I want to know whether this is ok or not,please give me suggestion.

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

bool Result = mainString.Contains(str1);

What about 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;

I don't have VS near me so I can't test it, but it should be something similar to this.


Need Your Help

parameter passing from a window to another

java javascript

i have this xhtml page represented in window1 ,

How to add the JDBC mysql driver to an Eclipse project?

java mysql servlets jdbc driver

I have downloaded the mysql-connector-java-5.1.24-bin.jar