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?
 
BuggyCoderConnect With a Mentor Commented:
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
 
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.