Link to home
Start Free TrialLog in
Avatar of Philippe Renaud
Philippe RenaudFlag for Canada

asked on

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....
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

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.
ged, they have 9 characters including the space
Avatar of Philippe Renaud

ASKER

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

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?
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 ?
ASKER CERTIFIED SOLUTION
Avatar of BuggyCoder
BuggyCoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi, BuggyCoder, I have a related question for you !


https://www.experts-exchange.com/questions/27644513/Help-manipulate-a-string.html


thanks!