Extract post numeric value from a string [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 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;

Related

Split string on every 500 values with comma [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
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..

Get String Between First n Character and Last n Charcter in 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 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.

Format a sting in based on Divider symbol [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 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

How can i generate unique number as format XXXX-XXXX-XXXX using 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
I want to generate number as format above and I try to use UUID() and GUID() but it is not what I want(difference format and it is hexadecimal not number)
anyone have any idea or logic to do it?
The simplest way to generate 1000000000000 unique numbers is just an ever-increasing sequence:
long i = 42; // whatever
string key = $"{i / 100000000:0000}-{(i / 10000) % 10000:0000}-{i % 10000:0000}";
The next time, increase i before generating the key.

How can I get the contents of a string before a particular character? [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 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);

Categories