Solved

# Help manipulate a string

Posted on 2012-03-22
Medium Priority
293 Views
This is a question related to :

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

the problem is if the string looks up first the second part of the string in the list

sbKeys will be inverted

for example:

lets say the list is :

Per1, Period 1
TY, This Year
LY, Last Year
Q1, This is Q1
Q2, This is Q2

and string is:   "This Year Period 1'

sbKeys will be:   "Per1_TY"

but the real result would need to be:  "TY_Per1"
like if it must be from left to right..

how could we modify the code ?
0
Question by:PhilippeRenaud
[X]
###### 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

LVL 15

Expert Comment

ID: 37754116
is this c# or vb
0

LVL 1

Author Comment

ID: 37754255
vb
0

LVL 6

Accepted Solution

effes earned 2000 total points
ID: 37754279
If the string you examine always contains just two parts replace
``````dictionary
.Keys
.ToList()
.ForEach(key =>
{
if (stringToMatch.ToLower().Contains(dictionary[key].ToLower()))
{
if (sbKeys.Length > 0)
sbKeys.Append(",");

sbKeys.Append(key);
}
});
``````
with
``````dictionary
.Keys
.ToList()
.ForEach(key =>
{
if (stringToMatch.ToLower().StartsWith(dictionary[key].ToLower()))
{
sbKeys.Append(key);
}
});

dictionary
.Keys
.ToList()
.ForEach(key =>
{
if (stringToMatch.ToLower().EndsWith(dictionary[key].ToLower()))
{
sbKeys.Append(",");
sbKeys.Append(key);
}
});
``````
(Sorry that it is C#, but it is based on the code from the old question.)
0

## Featured Post

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
###### Suggested Courses
Course of the Month11 days, 5 hours left to enroll