Splitting string into chunks of 2 C# [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 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);
}

Related

how do you take a string then organize the words in it with how many times the word occurs in c#? [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 5 years ago.
Improve this question
ok I have no idea on how to do this and i have tried looking up how to do this but nothing good came up so ill ask it here. So what i am trying to do is:
string input = TextEditor.text; <-- this is in windows form application and
The "TextEditor" is the textbox for input
i want to take the string (which is input from the texct box) then split it so each word is on every other line like so:
if input = "hi my name is is"
out put should be:
hi: 1
my: 1
name: 1
is: 2 <-- if the word is said it shouldn't be repeated.
could someone please help me? I am a true newbie and i am completely lost. I don't have any code yet because I DONT KNOW HOW TO DO THIS!
Use Linq GroupBy and Count:
string inputText = "hi my name is is";
var words = inputText.Split(' ').ToList();
var wordGroups = words.GroupBy(w => w).Select(grp => new {
Word = grp.Key,
Count = grp.Count()
});
string outputText = string.Join("\n", wordGroups.Select(g => string.Format("{0}:\t{1}", g.Word, g.Count)));
/*
hi: 1
my: 1
name: 1
is: 2
*/
Split the input string into an array, then use that to build a dictionary. If the word is already in the dictionary, increment it. Otherwise add it with an initial value of 1.
string input = TextEditor.text;
string[] inputWords = input.Split(' ');
Dictionary<string, int> wordCounts = new Dictionary<string, int>();
foreach (string word in inputWords)
{
if (wordCounts.ContainsKey(word))
{
wordCounts[word]++;
}
else
{
wordCounts.Add(word, 1);
}
}

how to find in between Ou=9999998 Only number [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 7 years ago.
Improve this question
How to find substring (Ou=9999998 only number) from string?
For example, suppose this is my string:
string myString = "cn=54445sddsfsd,ou=9998fgfgfgf8855,o=fgfgfdg,u=dfddfgfgg,subject=5454gffdgfg454‌​hg";
I want to only 99988855
I suggest converting the string to dictionary with a help of Linq:
using System.Collections.Generic; // for Dictionary
using System.Linq; // Linq: ToDictionary
...
String source =
"cn=54445sddsfsd,ou=99988855,o=fgfgfdg,u=dfddfgfgg.subject=5454gffdgfg454hg";
Dictionary<String, String> data = source
.Split(',')
.ToDictionary(line => line.Substring(0, line.IndexOf('=')),
line => line.Substring(line.IndexOf('=') + 1));
then get the value
// "99988855"; you may want to put int.Parse(data["ou"]); if you want integer value
String result = data["ou"];
or check if there's value and get it if it is:
String result;
if (data.TryGetValue("ou", out result)) {
// "ou" key found
}
else {
// no "ou" key found
}
Try this way you will get all the number part from this string after equal to symbol in an arry.
string myString = "cn=54445sddsfsd,ou=99988855,o=fgfgfdg,u=dfddfgfgg,subject=5454gffdgfg454hg";
string[] myArray= myString.Split(',');
string[] finalOutPut = (from item in myArray
where Regex.IsMatch(item.Substring(item.LastIndexOf('=') + 1), #"^\d+$")
select item.ToString()).ToArray<string>();
In this code the array finalOutput will have one value ie) ou=99988855.

how to use LINQ query? [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 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()));

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(",,","");

Count how many non-zero numbers are in string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I have this string:
0374:0462:0469:0354:0411:0433:0704:0391:0000:0000:0000:0000:0000:0000:0000:0000:
I would like to find out how many non-zero numbers (separated by ":") are inside it.
It should output "8".
var result = str.Split(':').Where(x => !x.All(c => c == '0')).Count();
Or you can use Trim alternatively:
var result = str.Split(':').Count(x => x.Trim('0').Any());
var count = input.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries).Count(x => int.Parse(x) != 0);
var test = src.Split(':').Count(c => c.All(char.IsDigit) && c.Any(x => x != '0'));

Categories