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

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'));

Related

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

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);
}

C# read only part of whole 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 a string which has a following format:
"####/xxxxx"
The text before the "/" is always an integer and I need to read it. How do I get only the integer part of this string (before the "/")?
Thank you for your help.
You can split the string on / and then use int.TryParse on the first element of array to see if it is an integer like:
string str = "1234/xxxxx";
string[] array = str.Split(new []{'/'}, StringSplitOptions.RemoveEmptyEntries);
int number = 0;
if (str.Length == 2 && int.TryParse(array[0], out number))
{
//parsing successful.
}
else
{
//invalid number / string
}
Console.WriteLine(number);
Use IndexOf and Substring:
int indexOfSlash = text.IndexOf('/');
string beforeSlash = null;
int numBeforeSlash = int.MinValue;
if(indexOfSlash >= 0)
{
beforeSlash = text.Substring(0, indexOfSlash);
if(int.TryParse(beforeSlash, out numBeforeSlash))
{
// numBeforeSlash contains the real number
}
}
Another alternative: use a regular expression:
var re = new System.Text.RegularExpression(#"^(\d+)/", RegexOptions.Compiled);
// ideally make re a static member so it only has to be compiled once
var m = re.Match(text);
if (m.IsMatch) {
var beforeSlash = Integer.Parse(re.Groups[0].Value);
}

Removing everything in a string before a specific phrase and after a specific phrase [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string and I want to delete everything before a phrase, and then delete everything after a different phrase. i.e.,
myString = "words words words FIRSTPHRASE these words I want SECONDPHRASE but not these words"
So the new string would be "these words I want".
Use String.Substring and String.IndexOf, it has also an overload with a start index:
string myString = "words words words FIRSTPHRASE these words I want SECONDPHRASE but not these words";
string result = myString;
int indexOfFirstPhrase = myString.IndexOf("FIRSTPHRASE");
if(indexOfFirstPhrase >= 0)
{
indexOfFirstPhrase += "FIRSTPHRASE".Length;
int indexOfSecondPhrase = myString.IndexOf("SECONDPHRASE", indexOfFirstPhrase);
if (indexOfSecondPhrase >= 0)
result = myString.Substring(indexOfFirstPhrase, indexOfSecondPhrase - indexOfFirstPhrase);
else
result = myString.Substring(indexOfFirstPhrase);
}
Demonstration
Something like this?
string theWordsIWant = Regex.Replace(myString, #"^.*?FIRSTPHRASE\s*(.*?)\s*SECONDPHRASE.*$", "$1");
Demonstration

How to split a formatted string? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
string text = "try your {{equal|even out|regularize} out|steady|tied|equal}";
Actually I want to split all words within the outer brackets ({}).
For example, if this is the string: "{{equal|even out|regularize} out|steady|tied|equal}", I want the split result array to be this:
{ equal,
even out,
regularize,
out,
steady,
tied,
equal }
string text = "try your {{equal|even out|regularize} out|steady|tied|equal} {champion|finest|top-quality}";
string1 = "try";
string2 = "your";
string3="{{equal|even out|regularize} out|steady|tied|equal}";
string4="{champion|finest|top-quality}";
How to split like this... please help me... Thanks !
try this :
string text = "try your {{equal|even out|regularize} out|steady|tied|equal}";
text = text.Remove(0, text.IndexOf('{'));
var array = text.Split('{', '}', '|');
array.ToList().ForEach(item =>
{
Console.WriteLine(item);
});
output :
equal
even out
regularize
out
steady
tied
equal

Categories