C# - What's the opposite of "IsNullOrWhiteSpace"? - c#

I am using an if condition with "IsNullOrWhiteSpace(+textbox)" to refer to a text filed that has no value or whitespace only. However, now I need to know hot to specify a field that is not empty or whitespace only.
This is the code I wrote:
if (string.IsNullOrWhiteSpace(pathofsrcfilesTOCOPY.Text))
But what if I want to specify to have the command run only when the textfield is NOT null or empty?
Thank you very much for any help. I am a beginner. Help is appreciated.
Have a great day!

string.IsNullOrWhiteSpace returns true if the text is null or whitespace. If the return value is false, then the text is populated with non-whitespace characters.
Search for where the condition is not true using !.
if (!string.IsNullOrWhiteSpace(pathofsrcfilesTOCOPY.Text))
this is the equivalent of:
if (string.IsNullOrWhiteSpace(pathofsrcfilesTOCOPY.Text) == false)
Both of the above will enter the if statement if pathofsrcfilesTOCOPY.Text is populated with non-whitespace text.

Worth noting, that although the accepted answer is correct, I find it easier to follow, when assigning its result to a variable and then evaluating that variable in the if condition.
bool isPathEmpty = string.IsNullOrWhiteSpace(pathofsrcfilesTOCOPY.Text);
if (!isPathEmpty)
{
//...
}

Related

checking an input number with regular expression

First, I wanna ask what is the difference between Regex.IsMatch and Regex.Match ?
Second, if I want to figure it out myself where should I go to? I mean, is there any help in c# to see what type does a method accept as input or how does it work at all?!!
Third, I wanna check the input to see if it contains only numbers.
And last, is there any website out there to give me regular expressions? It is a big story! I cannot memorize all the patterns and syntax!
Please dont try with Int32.TryParse() ! I wanna do it with Regex.
I tried this lines of code below:
string input = Int32.Parse(ConsoleReadLine().Trim());
public bool Validation (int myInt)
{
return Regex.Match(myInt, #"^[0 - 9] *$");
}
Here is the Windows Documentation for the Regex Class. IsMatch returns a boolean if the string matches the conditions. Match returns an instance of the RegularExpressions.Match class. In the above example, you should use the IsMatch Method.

Double.Parse fails for "10.00" retrieved value

The screenshot sums up the problem:
I have no control over the retrieved value. It comes in with some funky format that I can't figure out and the parsing fails even though it looks totally normal. Typing the value in manually works just fine.
How can I "normalize" the retrieved value so Decimal.Parse does not fail?
For reference, here is the string that fails (copied and pasted):
"‎10.00"
First I would check your regional settings to eliminate anything as simple as a difference in expected decimal separator.
If that draws a blank then if the string 10.00 parses successfully then a string that looks like 10.00 but which fails to parse cannot actually be 10.00.
Inspect and determine the character code of each character of the string and confirm that it really is 10.00 and not some exotic Unicode that has the same appearance but which is actually different (which may also include characters which are not even visible when displayed).
You might have some kind of special character hidden in the string you are retrieving.
Try this:
Double.Parse(Regex.Replace(decimalValue, #"[^0-9.,]+", ""))
You might need to add using statement for System.Text.RegularExpressions
I would replace only the one problematic character, that is the safest option:
s = s.Replace("\u200E", "");
As Jeroen Mostert mentioned in a comment, there is a non-printed character in your decimalValue.
This is a similar question which should help you deal with that.
https://stackoverflow.com/a/15259355/7636764
Edit:
Using the the string output = new string(input.Where(c => char.IsLetter(c) || char.IsDigit(c)).ToArray()); part of the solution, but also include in || char.IsPunctuation(c) after IsDigit will get your desired result.

IsNullEmptyOrWhiteSpace Yielding True when Text is ">="

I have a if statement that is doing validation for textbox on a WinForms application.
if (txtRule.Text.IsNullEmptyOrWhiteSpace())
{
result = false;
//error provider code
}
I know for a fact that the text in this textbox is the string ">=" because I'm using a breakpoint to figure out what the current text is in the textbox.
Obviously the text cannot be null since there is something in the textbox, and the same can be said about it not being empty. This means that is must be the case that the special characters ">=" are considered white space for some reason?
I would like to know the reason this if statement yields true when everything seems to be pointing towards a false value.
Considering that there is no instance method with that name in the System.String class (the most similar by name is String.IsNullOrWhiteSpace that is static, while you are using your method on a txtRule.Text that is a System.String), then probably that is an extension method written by someone where you work. Try doing a Go To Definition and check.

Catch text within a rounded bracket with quotes

I am still new to using Regex.
I'm on a project in C # and need to get a value that is within a bracket.
The Code is <$ Execute ['WebF'] $> I want to return the value WebF.
Agredeço much for your help.
#EDIT
My Code is <$Execute['WebF']$> without space
#EDIT
Thanks to doNet :).
Code that returned the value \<\$+Execute+\['(?<DATA>\w+)'\]+\$\>
\<\$\s+Execute\s+\['(?<DATA>\w+)'\]\s+\$\>
Run it with ExplicitCapture and IgnoreCase flags.

Regex to check whether "and,or,not,and not" in a word?

I have a seneario where i have to check a word contains "and,or,not,and not" but the regex which i have created fails. Can any body provide me the correct regex for this?
The regex which i have created is like this
Regex objAlphaPattern = new Regex(#"^[not|and not|and|not]");
if(objAlphaPattern.IsMatch(searchTerm))
{
//// code
}
But it always returns true.
I have tried the word "Pen and Pencil" and "Pen Pencil" but both returning true.. Can anybody help in providing correct regex?
You're starting with a begin anchor. If you don't want to only check if it happens at the beginning of the string then you shouldn't have the ^.
Also, you are using [] when you should be using (). Actually in this case you don't even need ().
[] indicates a character class. You just don't need that.
Regex objAlphaPattern = new Regex("\b(and|not)\b");
if(objAlphaPattern.IsMatch(searchTerm))
{
//// code
}
That should do the job.
I highly recommend The Regex Coach to help you build regex.
I also highly recommend http://www.regular-expressions.info/ as a reference.
EDIT:
I feel I should point out you don't really even need the object instance.
if(System.Text.RegularExpressions.Regex.IsMatch(searchTerm, "\b(and|not)\b"))
{
//// code
}
You can just use the static method.
That's a very good point Tim:
"\band\b|\bnot\b"
Another very good point stema:
"\b(and|not)\b"
try
(not)|(and not)|(and)
instead
Your regular expression is wrong, it should be (and|not). There is no need to check for and not either, since it will fail at the first and.
You can use an online tool to check your regular expressions; such as http://regexpal.com/?flags=&regex=(and|not)&input=Pen%20and%20Pencil

Categories