Solved

Help manipulating a String

Posted on 2012-03-22
9
332 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 39

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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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 39

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now