Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Help manipulating a String

Posted on 2012-03-22
9
335 Views
Last Modified: 2012-03-22
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....
0
Comment
Question by:PhilippeRenaud
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37752494
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
 
LVL 15

Expert Comment

by:Ess Kay
ID: 37752525
ged, they have 9 characters including the space
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 37752532
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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 15

Expert Comment

by:Ess Kay
ID: 37752551
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
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37752559
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
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37752590
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
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 37752668
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
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37752747
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
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 37753699
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

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question