Extract a substring from a string in C# - c#

How can I extract the substring "John Woo" from the below string in C#
CN=John Woo,OU=IT,OU=HO,DC=ABC,DC=com
Thanks !

You could use a Lookup<TKey, TElement>:
string text = "CN=John Woo,OU=IT,OU=HO,DC=ABC,DC=com";
var keyValues = text.Split(',')
.Select(s => s.Split('='))
.ToLookup(kv => kv[0], kv => kv.Last());
string cn = keyValues["CN"].FirstOrDefault(); // John Woo
// or, if multiple values with the same key are allowed (as suggested in the given string)
string dc = string.Join(",", keyValues["DC"]); // ABC,com
Note that you neither get an exception if the key is not present(as in a dictionary) nor if the key is not uniqe (as in a dictionary). The value is a IEnumerable<TElement>.

Try this
var regex = new Regex("CN=(?<mygroup>.*?),");
var match = regex.Match("CN=John Woo,OU=IT,OU=HO,DC=ABC,DC=com");
if(match.Success)
{
string result = match.Groups["mygroup"].Value;
}

Try this (this is a non generic answer) :
var name = str.Split(',').Where(n => n.StartsWith("CN=")).FirstOrDefault().Substring(3);

Something like this
var s = "CN=John Woo,OU=IT,OU=HO,DC=ABC,DC=com";
// this give you a enumarable of anonymous key/value
var v = s.Split(',')
.Select(x => x.Split('='))
.Select(x => new
{
key = x[0],
value = x[1],
});
var name = v.First().value; // John Woo

You can firstly split the string by the commas to get an array of strings, each of which is a name/value pair separated by =:
string input = "CN=John Woo,OU=IT,OU=HO,DC=ABC,DC=com";
var nameValuePairs = input.Split(new[] {','});
Then you can split the first name/value pair like so:
var nameValuePair = nameValuePairs[0].Split(new[]{'='});
Finally, the value part will be nameValuePair[1]:
var value = nameValuePair[1];
(No error handling shown above - you would of course have to add some.)

I created the below code of my own and finally got the substring I needed. The below code works for every substring that I want to extract that falls after "CN=" and before first occurrence of ",".
string name = "CN=John Woo,OU=IT,OU=HO,DC=ABC,DC=com";
int index1 = name.IndexOf("=") + 1;
int index2 = name.IndexOf(",") - 3;
string managerName = name.Substring(index1, index2);
The Result was "John Woo"
Thanks all for your help...

Related

How to parse INT values in brackets

How can I parse the INT contents of this string, and add to List ?
Files[file1.jpg[2066654],file2.png[234235],file3.gif[56476788]]
That way I can call fields, like list.Filename, list.Filesize
Its a custom string im using to group files in a txt file, so I want to extract these values out : filename (with extension), and size (int)
How can I do this? I've tried
var filename = str.Substring(str.LastIndexOf('Files[') + 1).str.Substring(str.LastIndexOf(']'));
But I have no way of getting this especially in this type of format. Any suggestions?
Thanks!
Regex is your friend here.
Try this:
var regex = new Regex(#"^Files\[((?'name'(.*?))\[(?'length'(\d+))\],?)*\]$");
var input = "Files[file1.jpg[2066654],file2.png[234235],file3.gif[56476788]]";
var match = regex.Match(input);
var names = match.Groups["name"].Captures.Cast<Capture>();
var lengths = match.Groups["length"].Captures.Cast<Capture>();
var output =
names
.Zip(lengths, (f, n) => new
{
file = f.Value,
length = int.Parse(n.Value)
})
.ToArray();
That gives me:
file
length
file1.jpg
2066654
file2.png
234235
file3.gif
56476788

Parse string to different variables c#

I've got a string fdf=232232&lid=19974832&number=1&aa_result1_1=someId1&aa_resuuuuuult2_2=someId2&aa_resuuuult3_3=someId3
and if aa exists I need to take values and add them to dictionary like:
var dict = extendedIds.Add("result1", new Dictionary<string, int[]>()
{
{
"someId1",
new int[]{ 1 }
}, ...
});
however I am having a difficult time deciding how to parse it properly? I need to accept multiple aa values (the ones that come as resultN, someIdN and a number (which is the number after resultN_NUMBER).
I tried to use substring but that doesn't work as I dont't now the length of word result
Basically it is
var parameters = $"pam=805700&laaid=19974832&kpm=1&{HttpUtility.UrlEncode("aa_{result}_{number}={id}&aa_{result}_{number}={id}&aa_{result}_{number}={id}", Encoding.UTF8)}";
So I decode it and get string:
var decoded = input.ToString().UrlDecode();
I need to accept multiple aa values, so in this example there would be three values, two of them comes from in bertween _ one after = but I wonder how to take these values then there could be something else also split by _...
also I could var parsed = HttpUtility.ParseQueryString(decoded); parse to NameValueCollection. but I can't use parsed.GetValues("aa") because the key would be e.g. aa_result1_1 and I never know beforehand what it is
this is a query string, you can use HttpUtility.ParseQueryString to parse it
see
https://learn.microsoft.com/en-us/dotnet/api/system.web.httputility.parsequerystring?view=net-5.0
Would this set you on the right track?
var qs = "fdf=232232&lid=19974832&number=1&aa_result1_1=someId1&aa_resuuuuuult2_2=someId2&aa_resuuuult3_3=someId3";
var nvc = System.Web.HttpUtility.ParseQueryString(qs);
foreach (var key in nvc.AllKeys.Where(k => k.StartsWith("aa")))
{
var id = nvc[key];
var parts = key.Split('_');
var result = parts[1];
var number = parts[2];
Console.WriteLine($"result = '{result}', number = '{number}' => id = '{id}'");
}
Use ParseQueryString to convert your string into a NameValueCollection.
Then use each key that starts with "aa"
Get its value - this is your "id"
Split the key on the _
Ignore the first part (which would be "aa") and use the next two parts
Of course you would want to add some safety: I now assume that there always are 3 parts in that key. Plus you want to do something useful with the results.
The above code prints this
result = 'result1', number = '1' => id = 'someId1'
result = 'resuuuuuult2', number = '2' => id = 'someId2'
result = 'resuuuult3', number = '3' => id = 'someId3'

Convert List Combo into Comma-Separated String in c#

My code is as below:
List<string> colorList = new List<string>();
....
sCombo = reader["Combo"].ToString();
colorList.Add(sCombo.ToString());
....
foreach (var Combo in colorList)
{
Response.Write(string.Join(",", Combo));
}
Output: D410D430D440D420 instead of D410,D430,D440,D420
What is the most simple way to convert the List<string> into a comma-separated string?
EDIT #01
Your suggestion working, but I need this new output :
'D410','D430','D440','D420'
Because use this string on sql query.
Thank you
I think this would be very handy
var colorList = new List<string>() { "D410", "D430", "D440", "D420" };
string commaSeparated = string.Join(",", colorList);
Console.WriteLine(commaSeparated);
or try solution based on Linq
Console.WriteLine(colorList.Select(s => s + ",").Aggregate((s, q) => s + q).TrimEnd(','));
The output
D410,D430,D440,D420
Edit
string result = string.Join(",", colorList.Select(e => "'" + e + "'"));
Console.WriteLine(result);
will give you
'D410','D430','D440','D420'
Without a foreach:
Response.Write(string.Join(",", colorList));
You need to output like this => 'D410','D430','D440','D420'
So try below,
string result = string.Join(",", colorList.Select(x => $"'{x}'"));
Response.Write(result);
What we did above?
Answer: First we flatten each item in your list with a single quoted ('') surrounding string and then we just pass this newly generated flatten result to join method of string with a comma (,) to get your desired output.
Output: (From Debugger)

Reading Text file with Linq with 2 patterns

I need to read a text file like this
MyItemName = Description # MoreInfo
Now I need to convert this 3 fields in to a table. using the '=' and '#' as pattern.
Just splitting on = and # - this returns and IEnumerable of an anonymous class with the properties you are interested in:
var items = File.ReadAllLines(fileName)
.Skip(1) //Skip header
.Where( line => !string.IsNullOrWhiteSpace(line))
.Select(line =>
{
var columns = line.Split('=', '#');
return new
{
ItemName = columns[0].Trim(),
Description = columns[1].Trim(),
MoreInfo = columns[2].Trim()
};
});
This approach would require the separator tokens to be used as separators exclusively - if they do occur in any of the fields, this will mess up everything and void this approach.
if you really want to use linq for it...
It doesn't look very nice and it doesn't create a table, but you get the point:
from line in File.ReadAllLines(filename)
let eqPos = line.IndexOf('=')
let atPos = line.IndexOf('#')
select new {
Name = line.Substring(0, eqPos).Trim(),
Desc = line.Substring(eqPos + 1, atPos - (eqPos + 1)).Trim(),
Info = line.Substring(atPos + 1).Trim()
}

how to use split to get the middle value which retrieve from datagridview

hihi
i have a question that i want to ask about c# and window form
i have this data... A,B,C; A1,B1,C1; A2,B2,C2; (this data is not hardcoded, it can continue to change, when more data insert come in) , i select this database column which is name as ColABC , and i retrieve and put in the datagridview ,
So is possible to always get the middle data?....but it is always this format record1 data, record1 data, record1; record2 data, record3 data, record...and so i want this data all the middle value retrieve out to this become this B,B1,B2.......so on...
ID | ColABC
1 | A,B,C; A1,B1,C1; A2,B2,C2;
This is like my DataGridView, the above.
The only thing i know is use split by ; first then by ,.
Is there any direct way?
Something like this, I try:
string[] doll = null;
doll = Convert.ToString(DGV.CurrentRow.Cells[0].Value).Split(';');
Basically like example the above code, doll get the " A,B,C; A1,B1,C1; A2,B2,C2; " this data which i retrieve from datagridview, so if i declare roll[0] it will give me "A,B,C", like what i mention above the data will change so how am i get the middle value always??
var input = "A,B,C; A1,B1,C1; A2,B2,C2;";
var resultList = Regex.Matches(input, #".*?,(.*?),.*?;")
.Cast<Match>()
.Select(arg => arg.Groups[1].Value)
.ToList();
var firstValue = resultList[0];
var secondValue = resultList[1];
// bind to a combobox
comboBox1.DataSource = resultList;
var comaSeparatedString = string.Join(",", resultList);
string testString = "A,B,C; A1,B1,C1; A2,B2,C2;";
Regex rgx = new Regex("[^,]+,([^,]+),[^,]+");
List<string> whatYouWant = testString.Split(';').Select(a => rgx.Replace(a, "$1")).ToList();
or
string testString = "A,B,C; A1,B1,C1; A2,B2,C2;";
Regex rgx = new Regex("[^,;]+,([^,;]+),[^,;]+;?");
for(Match m = rgx.Match(testString); m.Success; m = m.NextMatch())
{
Console.WriteLine(m.Groups[1].Value);
}
What about this.... Not in my device, to test this code... Still this is the approach...
foreach(DataGridViewRow dgvr in Datagridview.Rows){
if(dgvr!=null){
string middlevalues="";
string testString= dgvr.Cells[ColumnIndex].Value.ToString();
//If this string is string testString = "A,B,C; A1,B1,C1; A2,B2,C2;";
string[] basesplit = testString.Split(';');
int i=0;
foreach(string words in baseplit){
if(i<baseplit.Count-1){
middlevalues=words.Split(',')[1].ToString()+','
}else{ middlevalues=words.Split(',')[1].ToString();
i++;
}
}
}
}

Categories

Resources