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
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 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..
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 5 years ago.
Improve this question
i want to get post digits from "KB"(Actual data size)
for example
string word1="Product data 5KB per second"
string word2="Product data is 5 KB per hour"
i want to extract 5 from the above words if i pass KB
This Regex should work
var word2Size = Regex.Match(word2, #"\d+(?=\s?KB)").Value;
or for floating point numbers
var word2Size = Regex.Match(word2, #"\d+\.?\d+(?=\s?KB)").Value;
it should return a numeric value immediately preceding optional whitespace followed by KB
You could generate a regex string for different suffix with :
string regexString = String.Format(#"\d+\.?\d+(?=\s?{0})", mySuffix);
var word2Size = Regex.Match(word2, regexString).Value;
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 6 years ago.
Improve this question
I would like to exclude a specific string when it is contained in an expression:
Example:
myurl.htm = exclude
myurl = include
I tried this one : ([a-z0-9]+)(?!.htm)
But looks like it doesn't work.
Try the following:
([a-z0-9]+)(?!^\.htm)
You had two errors in your expression:
You have to escape the dot . with a backslash because it means "matches any character (except newline)" unescaped.
You have to add a ^ to prevent cutting of the last character.
You can test your expression on this website: https://regex101.com/
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 need one white space only in my regular expressen.
How should I create the code that validate if one white space is availabe in the string value?
I'm not sure exactly what you mean but I'm guessing you want to check for exactly one whitespace, but any number of non-whitespace characters:
#"^\S*\s\S*$"
Example code:
Regex regex = new Regex(#"^\S*\s\S*$");
Console.WriteLine(regex.IsMatch("Hello, world!"));
Console.WriteLine(regex.IsMatch("This contains three spaces."));
Console.WriteLine(regex.IsMatch("Two\nlines."));
Output:
True
False
True
Other variations
To check if the string contains exactly one whitespace only (no other characters):
#"^\s$"
To check if the string contains at least one whitespace:
#"\s"