Linq or better approach to extract values from string[] - c#

I have this input string and need to extract values of Uid, pwd and Dsn attributes...
I am splitting the values using ; char and then replacing Uid ="" and that's how I am reading values for other attributes too...
String[] test = "Uid=test;Pwd=abc;dsn=xxx".split(';')
id = test[0].Replace("Uid=", "");
pwd = test[0].Replace("Pwd", "");
datasrc = test[0].Replace("Dsn", "");
Is there any better approach to extract values from string[] ?

I suggest using Dictionary<String, String> and, yes, Linq to materialize the initial string into it:
string test = "Uid=test;Pwd=abc;dsn=xxx";
Dictionary<string, string> dict = test
.Split(';')
.Select(item => item.Split('='))
.ToDictionary(pair => pair[0], pair => pair[1], StringComparer.OrdinalIgnoreCase);
...
var id = dict["Uid"];
var pwd = dict["Pwd"];
var datasrc = dict["Dsn"]; // please, notice that the name is case insensitive

You can project after splitting and then split on = character and pick the second index, but it would only work, if the string is guaranteed to be always in this format, then this should work for you:
var result = "Uid=test;Pwd=abc;dsn=xxx".Split(';')
.Select(x=> x.Split('=')[1]);
Result:
See the working DEMO Fiddle

Related

TextBox display closest match string

How can I get the string from a list that best match with a base string using the Levenshtein Distance.
This is my code:
{
string basestring = "Coke 600ml";
List<string> liststr = new List<string>
{
"ccoca cola",
"cola",
"coca cola 1L",
"coca cola 600",
"Coke 600ml",
"coca cola 600ml",
};
Dictionary<string, int> resultset = new Dictionary<string, int>();
foreach(string test in liststr)
{
resultset.Add(test, Ldis.Compute(basestring, test));
}
int minimun = resultset.Min(c => c.Value);
var closest = resultset.Where(c => c.Value == minimun);
Textbox1.Text = closest.ToString();
}
In this example if I run the code I get 0 changes in string number 5 from the list, so how can I display in the TextBox the string itself?
for exemple : "Coke 600ml" Right now my TextBox just returns:
System.Linq.Enumerable+WhereEnumerableIterator`1
[System.Collections.Generic.KeyValuePair`2[System.String,System.Int32]]
Thanks.
Try this
var closest = resultset.First(c => c.Value == minimun);
Your existing code is trying to display a list of items in the textbox. I looks like it should just grab a single item where Value == min
resultset.Where() returns a list, you should use
var closest = resultset.First(c => c.Value == minimun);
to select a single result.
Then the closest is a KeyValuePair<string, int>, so you should use
Textbox1.Text = closest.Key;
to get the string. (You added the string as Key and changes count as Value to resultset earilier)
There is a good solution in code project
http://www.codeproject.com/Articles/36869/Fuzzy-Search
It can be very much simplified like so:
var res = liststr.Select(x => new {Str = x, Dist = Ldis.Compute(basestring, x)})
.OrderBy(x => x.Dist)
.Select(x => x.Str)
.ToArray();
This will order the list of strings from most similar to least similar.
To only get the most similar one, simply replace ToArray() with First().
Short explanation:
For every string in the list, it creates an anonymous type which contains the original string and it's distance, computed using the Ldis class. Then, it orders the collection by the distance and maps back to the original string, so as to lose the "extra" information calculated for the ordering.

Parse style attribute collection using linq

I want to parse an SVG style attribute, which is just a delimited string, e.g.
"fill:#e2b126;stroke:#010101;stroke-width:0.3177;stroke-miterlimit:10"
into a Dictionary<string,string> so that I can perform some processing on it.
Here's what I have, which does the job, but I'd like to make it neater using a linq projection, just can't seem to get the syntax. I tried using .Select().ToDictionary etc, but no joy. Thanks:
string attributes = "fill:#e2b126;stroke:#010101;stroke-width:0.3177;stroke-miterlimit:10";
var pairs = attributes.Split(';').ToList();
var dic = new Dictionary<string, string>();
pairs.ForEach(p =>
{
var pair = p.Split(':');
dic.Add(pair[0], pair[1]);
});
foreach (var k in dic.Keys)
{
Console.WriteLine(k + " " + dic[k]);
}
Expected output:
fill #e2b126
stroke #010101
stroke-width 0.3177
stroke-miterlimit 10
Try the following
string attributes = "fill:#e2b126;stroke:#010101;stroke-width:0.3177;stroke-miterlimit:10";
var map = attributes
.Split(new []{';'}, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Split(new []{':'}, StringSplitOptions.RemoveEmptyEntries))
.ToDictionary(p => p[0], p => p[1]);
Breakdown
The first Split call will return an array of String values where every entry is in the key:value format. The following Select call will convert every one of those entries into a string[] where the first element is the key and the second is the value. The ToDictionary call just expressly performs this mapping

Add keyvaluepair from file to Dictionary?

I am trying to import values from a .txt file into my dictionary. The .txt file is formatted like this:
Donald Duck, 2010-04-03
And so on... there is 1 entry like that on each line. My problem comes when I try to add the split strings into the dictionary.
I am trying it like this: scoreList.Add(values[0], values[1]); But it says that names doesn't exist in the context. I hope someone can point me in the correct direction about this...
Thank you!
private void Form1_Load(object sender, EventArgs e)
{
Dictionary<string, DateTime> scoreList = new Dictionary<string, DateTime>();
string path = #"list.txt";
var query = (from line in File.ReadAllLines(path)
let values = line.Split(',')
select new { Key = values[0], Value = values[1] });
foreach (KeyValuePair<string, DateTime> pair in scoreList)
{
scoreList.Add(values[0], values[1]);
}
textBox1.Text = scoreList.Keys.ToString();
}
Your values variable are only in scope within the LINQ query. You need to enumerate the query result, and add the values to the dictionary:
foreach (var pair in query)
{
scoreList.Add(pair.Key, pair.Value);
}
That being said, LINQ features a ToDictionary extension method that can help you here. You could replace your loop with:
scoreList = query.ToDictionary(x => x.Key, x => x.Value);
Finally, for the types to be correct, you need to convert the Value to DateTimeusing, for instance, DateTime.Parse.
First you are doing it wrong, you should add item from list not values[0] and values[1] used in LINQ..
Dictionary<string, DateTime> scoreList = new Dictionary<string, DateTime>();
string path = #"list.txt";
var query = (from line in File.ReadAllLines(path)
let values = line.Split(',')
select new { Key = values[0], Value = values[1] });
foreach (var item in query) /*changed thing*/
{
scoreList.Add(item.Key, DateTime.Parse(item.Value)); /*changed thing*/
}
textBox1.Text = scoreList.Keys.ToString();
The immediate problem with the code is that values only exists in the query expression... your sequence has an element type which is an anonymous type with Key and Value properties.
The next problem is that you're then iterating over scoreList, which will be empty to start with... and there's also no indication of where you plan to convert from string to DateTime. Oh, and I'm not sure whether Dictionary<,>.Keys.ToString() will give you anything useful.
You can build the dictionary simply enough though:
var scoreList = File.ReadLines(path)
.Select(line => line.Split(','))
.ToDictionary(bits => bits[0], // name
bits => DateTime.ParseExact(bits[1], // date
"yyyy-MM-dd",
CultureInfo.InvariantCulture));
Note the use of DateTime.ParseExact instead of just DateTime.Parse - if you know the format of the data, you should use that information.

Search for substring in an item of List

I have list :
List<string> str = new List<string>();
str.Add("asdf---US,IN");
str.Add("asdg---UK,IN");
str.Add("asjk---RU,IN");
str.Add("asrt---IT,DE");
I want to get List like ("asdf","asdg","asjk") when i enter "IN". For this i'm doing :
System.Text.RegularExpressions.Regex regEx =
new System.Text.RegularExpressions.Regex("asr",
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
List<string> str = new List<string>();
str.Add("asdf---US,IN");
str.Add("asdg---UK,IN");
str.Add("asjk---RU,IN");
str.Add("asrt---IT,DE");
var getArray = str.Where<string>(item => regEx.IsMatch(item)).ToList<string>();
str = getArray.ToList();
str is having correct result. but it is containing whole item like "asdf---US,IN","asdg---UK,IN", "asjk---RU,IN". I only want first four character in item in list i.e., 'asdf", "asdg", "asjk". What condition can i put in lambda expression, to get list i want?
If its only going to be first four characters use string.SubString:
var getArray = str.Where(item => regEx.IsMatch(item))
.Select(r=> r.Substring(0,4))
.ToList();
You also doesn't need to specify <string> cast with where and ToList. Items in your list are already of type string.
EDIT:
If you are only looking for those strings that ends with "IN" you may get rid of the regex and use string.EndsWith:
var getArray = str.Where(item => item.EndsWith("IN"))
.Select(r=> r.Substring(0,4))
.ToList();
The regex should be
.*?(?=---.*?IN)
and the query should be
var getArray = str.Where<string>(item => regEx.IsMatch(item)).Select<string>(item => regEx.Match(item)).ToList<string>();

Convert a delimted string to a dictionary<string,string> in C#

I have a string of the format
"key1=value1;key2=value2;key3=value3;"
I need to convert it to a dictionary for the above mentioned key value pairs.
What would be the best way to go about this?
Thanks.
Something like this?
var dict = text.Split(new[] {';'}, StringSplitOptions.RemoveEmptyEntries)
.Select(part => part.Split('='))
.ToDictionary(split => split[0], split => split[1]);
Of course, this will fail if the assumptions aren't met. For example, an IndexOutOfRangeException could be thrown if the text isn't in the right format and an ArgumentException will be thrown if there are duplicate keys. Each of these scenarios will require different modifications. If redundant white-space could be present, you may need some string.Trim calls as necessary.
Updated Ani's to take in account the semi colon at the end. The where clause will ensure that you have a key and value before creating and entry.
var dictionary = "key1=value1;key2=value2;key3=value3;"
.Split(';')
.Select (part => part.Split('='))
.Where (part => part.Length == 2)
.ToDictionary (sp => sp[0], sp => sp[1]);
You could do this using JSON string, for example:
var dic = JsonConvert.DeserializeObject<Dictionary<int, string>>("{'1':'One','2':'Two','3':'Three'}");
Behold the awesome whitespace ignoring, correcting for last value having or not having a semicolon power of regular expressions:
var dict = Regex.Matches("key1 = value1; key2 = value2 ; key3 = value3", #"\s*(.*?)\s*=\s*(.*?)\s*(;|$)")
.OfType<Match>()
.ToDictionary(m => m.Groups[1].Value, m => m.Groups[2].Value);
But seriously though, Ani deserves props for the .ToDictionary(). I would never have thought of that.
You could write it like this or loop over it to do it yourself. Either way. Ultimately, you're splitting on ; to get the item pairs, then on = to get the key and value.
string input = "key1=value1;key2=value2;key3=value3;";
Dictionary<string, string> dictionary =
input.TrimEnd(';').Split(';').ToDictionary(item => item.Split('=')[0], item => item.Split('=')[1]);
Loop version:
Dictionary<string, string> dictionary = new Dictionary<string, string>();
string[] items = input.TrimEnd(';').Split(';');
foreach (string item in items)
{
string[] keyValue = item.Split('=');
dictionary.Add(keyValue[0], keyValue[1]);
}

Categories