Help manipulating a String

Hello EE,

i have a Dictionnary(Of String, String) like this :


TY, This Year
LY, Last Year
Q1, This is Q1
Q2, This is Q2


Let's say I have  a string :   "Last Year This is Q2"

What I would like is to read the string and on first match inside Value of the dicitonnary (here first match would be   Last Year ,   I would like to get the Key   "TY" then continue because the string is not finish  then get This is Q2 and then go read the value to get the Key "Q2"

So a new string would be "TY Q2"

Can you help me ?

by the way it will always match... the only thing is that we cannot know at wich spaces the next word will be... but if we match "Last Year"   the rest of the string should be the next match 100% of the time....
LVL 1
PhilippeRenaudAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
String Part1 = theString.Substring(0,8);
String Part2 = theString.Substring(9);

Works out because both have 8 characters.

You can then do the lookups from there.
0
Ess KayEntrapenuerCommented:
ged, they have 9 characters including the space
0
PhilippeRenaudAuthor Commented:
How can I know its 8, lets say the word was "Last Year This" rather than 'Last Year"

I cannot know the number of caracters thats why im looking for a way to compare both and on a match it would be good
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Ess KayEntrapenuerCommented:
i would use the split function
and sp-lit on the comma



Creating arrays with the Split(split-character) function.

This function allows you to create a one-dimensional array, by splitting a string by
recognizing a certain character, then putting any text after the character on a new line in the array.

This code will pop up a message box For each item in the array, which is 4. Note that the first line is infact 0.

Dim i As String = "Line 0|Line 1|Line 2|Line 3"
Dim a() As String
Dim j As Integer
a = i.Split(",")
For j = 0 To a.GetUpperBound(0)
 MsgBox(a(j))
Next
0
BuggyCoderCommented:
Here is a sample code in c#:-

IDictionary<string, string> dictionary = new Dictionary<string, string>
                                                         {
                                                             {"TY", "This Year"},
                                                             {"LY", "Last Year"},
                                                             {"Q1", "This is Q1"},
                                                             {"Q2", "This is Q2"}
                                                         };

            string stringToMatch = "Last Year This Is Q2";
            var sbKeys = new StringBuilder();

            dictionary
                .Keys
                .ToList()
                .ForEach(key =>
                             {
                                 if (stringToMatch.ToLower().Contains(dictionary[key].ToLower()))
                                 {
                                     sbKeys.Append(key);
                                     sbKeys.Append(",");
                                 }
                             });

            Console.WriteLine(sbKeys);

Open in new window

0
Kyle AbrahamsSenior .Net DeveloperCommented:
by the way it will always match... the only thing is that we cannot know at wich spaces the next word will be... but if we match "Last Year"   the rest of the string should be the next match 100% of the time....

What I read from this is you will have

[Last/This] [Year] This is [Q1/Q2]

Or am I missing something?
0
PhilippeRenaudAuthor Commented:
BuggyCoder, it works great. there is just 2 small thing:

Its saying at End Function   that not all path returns  a value...

and also I have always a "," at the end   I know I could do remove at last index but is there a way to not have the last "," in another way ?
0
BuggyCoderCommented:
dictionary
                .Keys
                .ToList()
                .ForEach(key =>
                             {
                                 if (stringToMatch.ToLower().Contains(dictionary[key].ToLower()))
                                 {
                                     if (sbKeys.Length > 0)
                                         sbKeys.Append(",");  
                                     
                                     sbKeys.Append(key);
                                 }
                             });

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PhilippeRenaudAuthor Commented:
Hi, BuggyCoder, I have a related question for you !


http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_27644513.html


thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.