views:

97

answers:

5

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 ?

+2  A: 

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

Marcelo Cantos
@Marcelo Cantos,I have updated the question,it was mistaken.
Harikrishna
Cool, I've removed the additional remark.
Marcelo Cantos
A: 

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 + "(///|$)");

PierrOz
A: 

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

        }

    }

}
jpg
String.Split(char[]) splits on *any* of those characters - it doesn't treat it as a *sequence* of characters.
Jon Skeet
it does get a working hashtable with the individual items from the string enought to "find" the values
jpg
i said isnt pretty
jpg
A: 

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);
Harikrishna
A: 

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.

Frank