This question already has answers here:
Easier way of writing null or empty?
(7 answers)
How can I check whether a string variable is empty or null in C#? [duplicate]
(6 answers)
Closed 9 years ago.
In my app, I want to check if a string variable is empty.
I've handled it as follows,
if ((Name == null) || (Name == ""))
{
//Handled
}
But it passes this condition, if the value is given as " "(whitespace).
How can i detect if the variable contains only whitespaces??
Thanks in advance!
Use String.IsNullOrWhiteSpace:
if (String.IsNullOrWhiteSpace(Name))
{
//Handled
}
String.IsNullOrWhiteSpace(Name)
And MSDN article about it
Use String.IsNullOrEmpty to check null or empty and String.IsNullOrWhiteSpace to check null or whitespace
Use String.IsNullOrEmpty to check if it is null and String.IsNullOrWhiteSpace
to chack for whitespace!! For your case String.IsNullOrWhitespace(Name)
Related
This question already has answers here:
Find substring in a list of strings
(6 answers)
Closed 6 years ago.
I was wondering if it possible to check if a List contains part of a value. If it finds the value then return the value.
E.g. If the List had values 12345, 14567 and 14785, I want to search if the List contains '123',
Is this possible?
If it is can all values that contain '123' be returned?
This is how I add values to the List:
recordFailedPO.Add(Convert.ToInt32(dataGWHeight.Rows[0].Cells[0].Value));
This is how I'm checking for a part of a value:
if (recordFailedPO.Contains(currentPO))
{
// Code Here
}
Where currentPO is the user input.
Thanks for any help
In that case you could use string Contains method
var containsNumber = recordFailedPO.Where(x => x.ToString().Contains("123"));
try to convert to string before
int res = recordFailedPO.Find(x=>x.ToString().Contains(currentPO));
All values can be returned by FindAll .
List<int> res = recordPO.FindAll(x=>x.ToString().Contains(currentPO)):
This question already has answers here:
LinqToXml does not handle nillable elements as expected
(3 answers)
Closed 6 years ago.
The XElement has explicit support for casting to Nullable<int> but it's not working as I expected. The following unit test demonstrates the problem:
[TestMethod]
public void CastingNullableInt()
{
var xdoc = XDocument.Parse("<root xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><okay>123</okay><boom xsi:nil=\"true\"/></root>");
Assert.AreEqual(123, (int?)xdoc.Root.Element("okay"));
Assert.IsNull((int?)xdoc.Root.Element("boom"));
}
The test should pass the last assert. Instead it gives an FormatException:
Input string was not in a correct format.
Why doesn't it parse to null correctly here?
Linq to XML is not schema aware so it will not convert xsi:nil = "true" to a nullable variable. To test this you would need to do something like:
Assert.IsTrue((bool?)xdoc.Root.Element("boom").Attribute("{http://www.w3.org/2001/XMLSchema-instance}nil") == true);
XElement does not parse <boom xsi:nil=\"true\"/> correctly. It only works if you omit <boom xsi:nil=\"true\"/>, then the value is null and it returns (int?)null.
A workaround could be to first check on the value not being empty:
!string.IsNullOrEmpty((string)xdoc.Root.Element("boom"))
? (int?)xdoc.Root.Element("boom")
: null
;
You can check IsEmpty property:
var value = xdoc.Root.Element("boom").IsEmpty ? null : (int?)xdoc.Root.Element("boom");
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 7 years ago.
When using the following method there are use cases where one of the parameters, for this example RC_2 (datetime) may pass a null value. I need to have that null value converted into either a blank value or a string value so that it can be passed to a stringbuilder below is what I'm currently using,
public static string CHG_FixDatetime(string RC_1, string RC_2, string seperator)
{
var sb = new StringBuilder();
sb.Append(RC_1);
sb.Append(RC_2);
sb.Append(seperator);
DateTime dt;
if (!string.IsNullorWhiteSpace(RC_2))
sb.append(RC_2)
else
{
sb.Append(Convert.ToString(RC_2));
}
return sb.ToString();
}
I've tried multiple configurations and variations, but I'm unable to get past the System.NullException that is thrown when the RC_2 value is Null. Any help is appreciated.
Try the following solution
RC_2 = String.IsNullOrWhiteSpace(RC_2) ? "" : RC_2;
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why isn’t String.Empty a constant?
To make my code more readable I tried to assign String.Empty to a constant value:
const string PLATYPUS_ADDED_AND_ACCEPTED = string.Empty;
if (false) { }
else
{
toolTip = PLATYPUS_ADDED_AND_ACCEPTED;
}
but I get "the expression being added must be constant"
Isn't String.Empty always the same thing? That seems pretty constant to me.
string.Empty is a readonly field, not a constant.
The compiler has no way to know this will always be the same value.
This question already has answers here:
Easier way of writing null or empty?
(7 answers)
Closed 4 years ago.
How can I check whether a C# variable is an empty string "" or null?
I am looking for the simplest way to do this check. I have a variable that can be equal to "" or null. Is there a single function that can check if it's not "" or null?
if (string.IsNullOrEmpty(myString)) {
//
}
Since .NET 2.0 you can use:
// Indicates whether the specified string is null or an Empty string.
string.IsNullOrEmpty(string value);
Additionally, since .NET 4.0 there's a new method that goes a bit farther:
// Indicates whether a specified string is null, empty, or consists only of white-space characters.
string.IsNullOrWhiteSpace(string value);
if the variable is a string
bool result = string.IsNullOrEmpty(variableToTest);
if you only have an object which may or may not contain a string then
bool result = string.IsNullOrEmpty(variableToTest as string);
if (string.IsNullOrEmpty(myString))
{
. . .
. . .
}
string.IsNullOrEmpty is what you want.
Cheap trick:
Convert.ToString((object)stringVar) == ""
This works because Convert.ToString(object) returns an empty string if object is null. Convert.ToString(string) returns null if string is null.
(Or, if you're using .NET 2.0 you could always using String.IsNullOrEmpty.)