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;
Related
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);
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]);
}
This question already has answers here:
Regular Expression Groups in C#
(5 answers)
Closed 4 years ago.
string ABC = "This is test AZ12346";
Need value that occurs after AZ. AZ will always be present in the string and it will always be the last word. But number of characters after AZ will vary.
Output should be : AZ123456
Simply :
ABC.Substring(ABC.LastIndexOf("AZ"));
You can try LastIndexOf and Substring:
string ABC = "This is test AZ12346";
string delimiter = "AZ";
// "AZ123456"
string result = ABC.Substring(ABC.LastIndexOf(delimiter));
In case delimiter can be abscent
int p = ABC.LastIndexOf(delimiter);
string result = p >= 0
? ABC.Substring(p)
: result; // No delimiter found
If you are looking for whole word which starts from AZ (e.g. "AZ123", but not "123DCAZDE456" - AZ in the middle of the word) you can try regular expressions
var result = Regex
.Match(ABC, #"\bAZ[A-Za-z0-9]+\b", RegexOptions.RightToLeft)
.Value;
This question already has answers here:
Find and extract a number from a string
(32 answers)
Closed 8 years ago.
I have some strings like "pan1", "pan2", and "pan20" etc. I need to extract number. I use it:
char ch = s[(s.Length) - 1];
int n = Convert.ToInt32(Char.GetNumericValue(ch));
But in case of, for example, "pan20" the result is not correct 0.
Index Approach
if you know where is the starting index of the number then simply you can do this :
string str = "pan20";
int number = Convert.ToInt32(str.Substring(3));
Note that "3" is the starting index of the number.
Fixed Prefix Approach
try to remove "pan" from the string; like this
string str = "pan20";
int number = Convert.ToInt32(str.Replace("pan", ""));
Regular Expression Approach
use regular expression only when string contains undetermined text inside
string str = "pan20";
int number = Convert.ToInt32(System.Text.RegularExpressions.Regex.Match(str, #"\d+").Value;
You can use for example regular expressions, for example [0-9]+$ to get the numbers in the end. See the Regex class in MSDN.
This question already has answers here:
Detecting whitespace in textbox
(4 answers)
Closed 4 years ago.
Is there a way to determine if a string has a space(s) in it?
sossjjs sskkk should return true, and sskskjsk should return false.
"sssss".Trim().Length does not seem to work.
How about:
myString.Any(x => Char.IsWhiteSpace(x))
Or if you like using the "method group" syntax:
myString.Any(Char.IsWhiteSpace)
If indeed the goal is to see if a string contains the actual space character (as described in the title), as opposed to any other sort of whitespace characters, you can use:
string s = "Hello There";
bool fHasSpace = s.Contains(" ");
If you're looking for ways to detect whitespace, there's several great options below.
It's also possible to use a regular expression to achieve this when you want to test for any whitespace character and not just a space.
var text = "sossjj ssskkk";
var regex = new Regex(#"\s");
regex.IsMatch(text); // true
Trim() will only remove leading or trailing spaces.
Try .Contains() to check if a string contains white space
"sossjjs sskkk".Contains(" ") // returns true
This functions should help you...
bool isThereSpace(String s){
return s.Contains(" ");
}