how to use LINQ query? [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a input string,
string str1 = "aabcccabdfa";
I want to count the occurrence of each character and put it next to that character. So I need to output result as "a4b2c3d1f1". How can i achieve this with LINQ?
I try with "Dictionary" to do that, but not able to do that.
Thanks,

Do it like this:
string str1 = "aabcccabdfa";
string.Concat(str1.GroupBy(c => c).OrderBy(c => c.Key).Select(c => c.Key.ToString() + c.Count()));

string str1 = "aabcccabdfa";
var result=string.Concat(str1.GroupBy(a=>a)
.Select(a=>a.Key.ToString()+a.Count().ToString()));

string str1 = "aabcccabdfa";
var output = "";
str1.ToArray().Distinct().ToList().ForEach(x => output += x + (str1.Count(f => f == x).ToString()));

Related

How to crop strings using regular expressions? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Here's the phrase I'd like to convert.
Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC
I wanna pick up these words Summoner1, Summoner2, Summoner3, Summoner4.
I suppose I should detect the strings "joined the lobby", and ": " using regular expression(regex) but I have no clue how to.
Thank you in advance.
Codes for extra question.
var list = #"Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC";
var result = string.Join("|", list.Select(x => Regex.Replace(x, "( |:).*", string.Empty)));
If you want to use Regex, you can use the following.
"( |:).*"
Example,
var list= #"Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC";
var result = list.Split(new []{Environment.NewLine},StringSplitOptions.RemoveEmptyEntries).Select(x=> Regex.Replace(x,"( |:).*",string.Empty));
Update: Based on Comment
var result = string.Join("|",list.Split(new []{Environment.NewLine},StringSplitOptions.RemoveEmptyEntries).Select(x=> Regex.Replace(x,"( |:).*",string.Empty)));
Output
Summoner1|Summoner2|Summoner3|Summoner4
string sourc = #"Summoner1 joined the lobby.
Summoner2 jonied the lobby.
Summoner3: Top
Summoner4: ADC";
Regex reg = new Regex("^[\\s]*(Summoner[\\d])+.*$", RegexOptions.Multiline);
var result = reg.Matches(sourc).ToList().Select(x => x.Groups[1].Value).ToList();

Splitting string into chunks of 2 C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the following string:
string input ="this is a testx";
I need to remove the spaces and then split the input into chunks of two, so I can process every two letters individually:
th is is at es tx
I tried to remove the spaces with:
input=input.Remove(input.IndexOf(' '),1);
Then I couldn't do much with the splitting...
IEnumerable<string> output = input
.Replace(" ", string.Empty)
.Select((ch, i) => new{ch, grp = i/2})
.GroupBy(x => x.grp)
.Select(g => string.Concat(g.Select(x => x.ch)));
or more sensibly :)
input = input.Replace(" ", string.Empty);
IEnumerable<string> output =
Enumerable.Range(0, input.Length / 2).Select(x => input.Substring(x * 2, 2));
you can use the output as follows:
foreach(var item in output)
{
Console.WriteLine(item);
}

Replace the particular part of string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to replace particular string in another string if it contain that word . example
give string is "asp,mvc,c#,wpf" and another string is "<b>asp</b>,<b>wpf</b>" and my final result should be "<b>asp</b>,mvc,c#,<b>wpf</b>" , i have no idea about how to do it in c# code .please help me.
You can do this:
var str = "asp,mvc,c#,wpf";
var anotherStr = "<b>asp</b>,<b>wpf</b>";
var myArr = anotherStr.Replace("<b>", "").Replace("</b>", "").Split(',');
foreach (string value in myArr)
{
str = str.Replace(value, "<b>" + value + "</b>");
}
Console.WriteLine(str);

Move the selected string to first in comma separated string c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Move the selected string to first in comma separated string c#
If my csvstring is : C1,C2,C3
if my selectedstring is : C2
I require an output of : C2,C1,C3
Using the expression power of LINQ:
string cvsstring = "C1,C2,C3";
string selected = "C2";
cvsstring = string.Join(
",",
new[] { selected }.Concat(cvsstring.Split(',').Except(selected));
If there are more than one selected instances, the duplicated will be removed by this code. As the question is very unprecise about that I won't give a solution (using OrderBy, for example).
You can split, sort and join:
string csv = "C1,C2,C3";
string selected = "C2";
csv = String.Join(',', csv.Split(',').OrderBy(s => s == selected ? 0 : 1));
How about this?
var text ="C1,C2,C3";
var splits = text.Split(',');
var results =
String.Join(",",
Enumerable.Concat(
splits.Where(x => x == "C2"),
splits.Where(x => x != "C2")));
Maybe something like this:
var str= "C1,C2,C3";
var selected="C2";
var result= string.Join(",", str.Split(',').OrderBy (s =>s==selected?0:1));
It is pretty easy. Try below given code:
string csvstring = "C1,C2,C3";
string seletedstring = "C2";
string outputstring = selectedstring + csvstring.Replace(seletedstring .ToString(),"");
outputstring = outputstring.Replace(",,","");

how can i remove "/ ONLY" from "Rs1500/ONLY",using c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
string oldstring = textBox9.Text;
string newstring = oldstring.Remove(0, 2);
string o = newstring.Remove(4, 7);
Now, I want to get only "1500",rest of the things to be removed.
How can I do this? Please help me.
Try the following code
string newstring = Regex.Replace(oldstring, #"[^\d]", "");
It shall work.
there are number of ways to do this.. you can use Split() as below:
string oldstring = "Rs1500/ONLY";
string[] newstring = oldstring.Split('/');
string o = newstring[0];
This will give you "RS 1500" as result.
And if you want to remove RS as well then just add the below code in last of the above code:
string final = newstring[0].ToString().Replace("Rs","");
That's All
You can use Replace
string oldstring = textBox9.Text;
string newstring = oldstring.Replace("Rs","").Replace("/ONLY","");
This should give you only 1500
Try this way
string ss = "Rs1500/ONLY";
string[] newss = ss.Split('/');
ss = newss[0].ToString().Replace("Rs","");//Or try string.Empty() for instead of ""
See this demo code : with output

Categories