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
how to split this string on every 500 values ending in comma
string val= "1,2,3,4,5,.....................,2000,2001";
//Solution
[1,2,3,...,500]
[501,502,503,...,1000]
[1001,1002,...,1500]
You can match the string with the following regex instead (where 499 is 500 minus 1):
(?:[^,]+,){0,4}[^,]+
Demo (for splitting at every 5 commas here): https://regex101.com/r/nbRxdv/2
Assuming by "no loops" you mean you're happy to let LINQ use loops internally, maybe something like this:
string s = "your,comma,string";
string[] ss = s.Split(',');
Print500(ss, 0);
private void Print500(IEnumerable<string> ies, int skip)
{
if (skip > ies.Count())
return;
Console.Out.WriteLine(string.Join(",", ies.Skip(skip).Take(500)));
Print500(ies, skip + 500);
}
I haven't run it, so it might have some minor issues..
Related
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 3 years ago.
Improve this question
I've got a bit of a challenge. I have a string which contains of 11 characters, the first 10 denotes an amount, and the last one is a letter or special letter, which represents a value to add.
For example if I got
0000000400A
this means
400 + 2
So the result should be
402
I can't figure out how to do this. Any ideas?
Something like this?
// Setup
var dict = new Dictionary<char, int>();
dict.Add('A', 2);
dict.Add('B', 3);
dict.Add('C', 4);
dict.Add('D', 5);
var str = "0000000400A";
// This is what you need
var result = Convert.ToInt32(str.Substring(0, 10)) + dict[str[10]];
Console.WriteLine(result);
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 4 years ago.
Improve this question
My String: draw.text("hello",11,22);
Need Output: 11
So i need to get a String Between first , character and Last , character.
Its not duplicate question.I not found same questions.
I need to get the 2nd argument in my string,the string between first , and last , in string
Split your string on the , character, remove the first and last item, and join the remaining components back together:
var input = "draw.text(\"hello\",11,22);";
var components = input.Split(',');
var result = String.Join(",", components.Skip(1).Take(components.Length - 2));
// yields 11
Note: using System.Linq is required for Skip and Take.
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 4 years ago.
Improve this question
i have a string like this
4366|2d53|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|3f80|0|4248|295f|41c4|19c4|0|268
Here,
| - is denoted as a divider i want to make each divider consists exactly 4 charcters
Ex: 0|0 this must to be simplified as 0000|0000
12|13 this must to be simplified as 0012|0013
Note:
if the char. enclosed with | is less then 4 then add zero in front of the characters i.e. 12|13 is simplified as 0012|0013 not like this 1200|1300
Result:
4366|2d53|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|3f80|0|4248|295f|41c4|19c4|0|268
i want to convert this as
4366|2d53|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|3f80|0000|4248|295f|41c4|19c4|0000|2680
That can be solved with simple string operations Split(), PadLeft() and Join()
string input = "4366|2d53|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|3f80|0|4248|295f|41c4|19c4|0|268";
string result = string.Join("|",input.Split('|').Select(x => x.PadLeft(4, '0')));
Something like this should work:
string.Join("|", myString.Split('|').Select(x => x.PadLeft(4,'0')));
I made a fiddle: https://dotnetfiddle.net/uK1kyr
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 7 years ago.
Improve this question
I got something like this:
string s="Solid;Solid;Gass;Solid;Solid;Gass;Solid;Gass;Liquid;Liquid;"
and now I want to get rid of the copies in the string...so that in the end s should be like this:
s="Solid;Gass;Liquid;"
Try this:
var parts = s.Split(';');
var distinctParts = parts.Distinct();
var newString = string.Join(";", distinctParts);
Where:
Split will give you an array with all the words of your string, taking the specified character as the word separator (; in this case).
Distinct will give your a collection with the unique words of your array.
Finally, Join composes a new string with the unique words, using the specified string (;in this case) as the separator.
You can split the string, then find the distinct instances and join them back in one line:
string s = "Solid;Solid;Gass;Solid;Solid;Gass;Solid;Gass;Liquid;Liquid;";
s = string.Join(";", s.Split(';').Distinct());
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 8 years ago.
Improve this question
I want to get the previous chars from a certain char in a string.
For example: myString = "26+", so I want to get 26 without +. How can I do this?
Use Substring and IndexOf.
string str = "26+";
string requiredString = str.Substring(0, str.IndexOf('+'));
strings are immutable in C# and you have to assign the results to string itself or some other string.
The function String.Substring will do what you need.
yourString.Substring(0, keepCharactersBeforeHere);