string to float [duplicate] - c#

This question already has answers here:
How can I make part of regex optional?
(2 answers)
Closed 3 years ago.
I am trying to convert a string into a float.
I have a string that consists out of a number and some letters, I am using regex to remove the letters.
This is what I currenlty have:
string x = "0.5AA";
Console.WriteLine(float.Parse(Regex.Match(x.ToString(), #"(\d)+\.(\d+)").Value.Replace('.', ',')));
The output is: 0.5
This works if the string looks like 0.5AA, if the string is 100AA it crashes, is there a way to convert the 100AA to 100.0AA?

Try with this regex:
#"(\d)+(\.(\d)+)?"
It will optionally include the floating if they are there
Update 1
If you want to add the +- optionally change it to the following
[+-]?(\d)+(\.(\d)+)?

You can use Regex as below for select only numbers and not analphabets and then parse as float.
string x = "100AA";
string numString = Regex.Replace(x, "[^0-9.]", "");
Console.WriteLine(numString);
float y = float.Parse(numString);
Console.WriteLine(y);

Related

How to delete diacritics from a string in C#? [duplicate]

This question already has answers here:
How do I remove diacritics (accents) from a string in .NET?
(22 answers)
Closed 9 months ago.
I have a string -
125DF885DF44é112846522FF001
I want to remove é from the string. When I search online I get solutions to remove the accents from é and returns e.
The diacritic character can come anywhere in the string and not in fixed place, also can be more than one.
How do I remove those?
You can use this
string s = "125DF885DF44é112846522FF001";
string s1 = s.Replace("é","");
In general case, we can remove symbols of unicode NonSpacingMark range:
We turn each symbol into pair: symbol + its mark(s) (that's the diacritics)
We remove marks
Combine symbols back
Code:
using System.Linq;
...
string source = "125DF885DF44é112846522FF001";
string result = string.Concat(source
.Normalize(NormalizationForm.FormD)
.Where(c => CharUnicodeInfo.GetUnicodeCategory(c) !=
UnicodeCategory.NonSpacingMark))
.Normalize(NormalizationForm.FormC);

How can I create a string by combining two strings and using substitution? [duplicate]

This question already has an answer here:
How can I substitute the value of one string into another based on a substitution character?
(1 answer)
Closed 3 years ago.
I have code that creates two strings:
var string1 = "ま|ちが|#";
var string2 = "間|違|う";
I'm looking for a way to combine these such the resulting output contains the characters from string1 but if the character is a "#" then it takes the alternate character from string2.
string1 string2 desired output
ま|ちが|# 間|違|う まちがう
な|# 為|る なる
で|き|# 出|来|る できる
Does anyone have any suggestions as to how I can do this?
You can use IndexOf() method like
var hashIndex = string1.IndexOf('#');
if(hashIndex > 0) {
Console.WriteLine(string1.Substring(0, string1.Length - 2) + string2[hashindex]);
}

C#: I get CS1002 and CS1525 error because of qoutes - how to avoid that? [duplicate]

This question already has answers here:
Escape double quotes in a string
(9 answers)
Closed 5 years ago.
I have this simple code:
var q = "this is a test";
return q.Length;
Just that sometimes I need to be able to insert string that contains double quotes and this yields those errors
var q = "this is "not" a test";
How can I avoid that without manually escaping chars? I need something like python has its triple quotes when you can include anything
q = '''I can include 'everything' in here as long it's not triple quotes'''
you can state a # at the beginning of your string:
var q = #"this ""is"" how you do it.";

Find and extract all numbers from the String [duplicate]

This question already has answers here:
Finding all numbers in a string
(2 answers)
Closed 9 years ago.
I have a string like: "Hello I'm 43 years old, I need 2 burgers each for 1.99$".
I need to parse it and get all the numbers in it as double. So the function should return an array of values like: 43, 2, 1.99. In C++ I should've write all by myself, but C# has Regex and I think it may be helpful here:
String subjectString = "Hello I'm 43 years old, I need 2 burgers each for 1.99$";
resultString = Regex.Match(subjectString, #"\d+").Value;
double result = double.Parse(resultString);
After this, the resultString is "43" and result is 43.0. How to parse the string to get more numbers?
Your regex needs to be a little more complex to include decimals:
\d+(\.\d+)?
Then you need to get multiple matches:
MatchCollection mc = Regex.Matches(subjectString, "\\d+(\\.\\d+)?");
foreach (Match m in mc)
{
double d = double.Parse(m.Groups[0].Value);
}
Here is an example.
Try to use the following regular expression:
-?[0-9]+(\.[0-9]+)?
and then use Regex.Matches and iterate over the matches returned.
You should use Matches method to get a collection of matches. Also, you need to add dot to your regex
String subjectString = "Hello I'm 43 years old, I need 2 burgers each for 1.99$";
var matches = Regex.Matches(subjectString, #"\d+(\.\d+)?");
for (int i = 0; i < matches.Count; i++ )
{
double d = double.Parse(matches[i].Value);
}

Regex Limit the count of characters in a string [duplicate]

This question already has answers here:
How would you count occurrences of a string (actually a char) within a string?
(34 answers)
Closed 9 years ago.
Here is my string
string countCommas = 12,34,56
I am looking for REGEX for algorithm below
BOOL isCountExaclty2 = if(number of commas in string == 2){return TRUE;}else return FALSE
I want the right hand expression as one single REGEX expression which returns either TRUE or FALSE but not the count
(I know to use Regex.COUNT..but it ends up in 2 statements)
If you're looking for a pattern that will only match if there's exactly two commas in the string, this should work:
bool isCountExactly2 = Regex.IsMatch("12,34,56", "^([^,]*,){2}[^,]*$");
But regular expressions really aren't the right tool for this job.
Try this :
string countCommas = "12,34,56"
bool isCountExaclty2 = Regex.Split(countCommas, ",").Length == 2;

Categories

Resources