|
|
Not clicking it, but I'd use the C preprocessor.
Just because.
|
|
|
|
|
I like to write little libraries for code re-use:
using System;
using System.Globalization;
namespace WHATEVER
{
public static class StringExtensions
{
private static double dbl;
private static int i;
public static StringState GetStringState(this string str)
{
if (String.IsNullOrEmpty(str))
{
return str == "" ? StringState.IsEmpty : StringState.IsNull;
}
else
{
if (String.IsNullOrWhiteSpace(str)) return StringState.IsWhiteSpace;
}
if (Double.TryParse(str, out dbl)) return StringState.IsNumeric;
return StringState.IsStandard;
}
public static NumberState GetStringNumberState(this string str)
{
if (str.GetStringState() != StringState.IsNumeric) return NumberState.NotNumber;
if (Math.Abs(dbl % 1) < Double.Epsilon)
{
return NumberState.IsInt;
}
else
{
return NumberState.IsDouble;
}
}
public static bool StringIsHex(this string str)
{
if (str.StartsWith("0x")) str = str.Remove(0,2);
return Int32.TryParse(str, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out i);
}
}
public enum StringState
{
IsStandard = 0x0 << 0,
IsNull = 0x1 << 0,
IsEmpty = 0x1 << 1,
IsWhiteSpace = 0x1 << 2,
IsNumeric = 0x1 << 3,
}
public enum NumberState
{
NotNumber = 0x0 << 0,
IsInt = 0x1 << 0,
IsDouble = 0x1 << 1
}
} Sample tests:
string s1 = null;
string s2 = "";
string s3 = "12";
string s4 = "23.45";
string s5 = "0xFF";
string s6 = "23.45";
var state1 = s1.GetStringState();
var state2 = s2.GetStringState();
var state3 = s3.GetStringState();
var state4 = s4.GetStringState();
var state5 = s5.GetStringState();
var state6 = s6.GetStringState();
var nstate1 = s1.GetStringNumberState();
var nstate2 = s2.GetStringNumberState();
var nstate3 = s3.GetStringNumberState();
var nstate4 = s4.GetStringNumberState();
var ishex1 = s5.StringIsHex();
var ishex2 = "0xddDFf".StringIsHex();
var ishex3 = "0xXdDFf".StringIsHex();
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
Using static variables to store state between method calls could be problematic. I'd be inclined to replace them with either out parameters or value tuple[^] returns. Eg:
public static class StringExtensions
{
public static StringState GetStringState(this string str, out double dbl)
{
dbl = double.NaN;
if (str is null) return StringState.IsNull;
if (str.Length == 0) return StringState.IsEmpty;
if (string.IsNullOrWhiteSpace(str)) return StringState.IsWhiteSpace;
if (!double.TryParse(str, out var d)) return StringState.IsStandard;
dbl = d;
return StringState.IsNumeric;
}
public static StringState GetStringState(this string str) => GetStringState(str, out _);
public static NumberState GetStringNumberState(this string str, out double dbl)
{
if (GetStringState(str, out dbl) != StringState.IsNumeric) return NumberState.NotNumber;
if (Math.Abs(dbl % 1) < double.Epsilon) return NumberState.IsInt;
return NumberState.IsDouble;
}
public static NumberState GetStringNumberState(this string str) => GetStringNumberState(str, out _);
public static bool StringIsHex(this string str, out int i)
{
if (str.StartsWith("0x")) str = str.Remove(0, 2);
return int.TryParse(str, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out i);
}
public static bool StringIsHex(this string str) => StringIsHex(str, out _);
} Or:
public static class StringExtensions
{
public static (StringState state, double dbl) GetStringState(this string str)
{
if (str is null) return (StringState.IsNull, double.NaN);
if (str.Length == 0) return (StringState.IsEmpty, double.NaN);
if (string.IsNullOrWhiteSpace(str)) return (StringState.IsWhiteSpace, double.NaN);
if (!double.TryParse(str, out var d)) return (StringState.IsStandard, double.NaN);
return (StringState.IsNumeric, d);
}
public static (NumberState state, double dbl) GetStringNumberState(this string str)
{
switch (GetStringState(str))
{
case var n when n.state != StringState.IsNumeric:
{
return (NumberState.NotNumber, double.NaN);
}
case var n when Math.Abs(n.dbl % 1) < double.Epsilon:
{
return (NumberState.IsInt, n.dbl);
}
case var n:
{
return (NumberState.IsDouble, n.dbl);
}
}
}
public static (bool isHex, int value) StringIsHex(this string str)
{
if (str.StartsWith("0x")) str = str.Remove(0, 2);
return (int.TryParse(str, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var i), i);
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks, Richard; I'll study your comment and code carefully.
cheers, Bill
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
cool!
|
|
|
|
|
For mine a string.HasValue() is less explit in intention
- it doesn't really speak to what happens if string itself is NULL (as compared to the empty string "".)
- or does it mean string (itself) has a value, or THE string has a value? and is empty string not a "value": the value is AN empty string.
!string.IsNullOrEmpty() yes more wordy/longer but it does specifically talk to both NULL and the empty string.
btw: I find myself using string.IsNullOrWhiteSpace() more often which one could argue doesn't speak to the empty string: i.e. "" is not NULL but pedantically nor is it white space.
... they should rename that: string.IsNullOrEmptyOrWhiteSpace(),
and why not: string.IsNotNullNorEmptyNorWhitespace() ???
and just for completion: string.IsNullOrEmptyOrWhiteSpaceOrSpecials() / string.IsNotNullNorEmptyNorWhitespaceNorSpecials()
modified 18-Feb-19 23:32pm.
|
|
|
|
|
In my case, I think the function is explainable to the team. The goal was to inverse the !string.IsNullOrEmpty to read positive. It is an attempt to follow how nullable booleans, integers, etc.. contain a HasValue function.
Again, the IsNot still sounds "negative" to me which was the goal of the extension method.
|
|
|
|
|
I'd agree with Lopati - IsNullOrEmpty is a specific question (as is IsNullOrWhitespace) which specifies exactly what conditions it checks for: HasValue doesn't, it implies a degree of "valueness" which is not implicit to a string, but is added by context. HasValue could mean "has a valid numeric", "has a name and address" which "IsNullOrEmpty" doesn't - it's a basic generic check which prepares the way for more explicit "value" checking.
I'll stick with IsNullOrWhitespace myself!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
OriginalGriff wrote: I'll stick with IsNullOrWhitespace myself! Only because you can, it doesn't mean you should
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
You can use extension methods for all sorts of test (e.g. comparisons) to handle null arguments, so you don't have to explicitly test for null (or cause an exception when a null occurs).
This is obviously a good idea only if a null value is meaningful in the context. E.g. rather than
if (middleName != null && middleName.Equals("Peter")) ...
I write
if (middleName.Eq("Peter")) ...
with the Eq extension method checking the this argument for null before using it in an Equals test.
|
|
|
|
|
Another option would be to use the null conditional operator:
if (name?.Length >= 3) But the null conditional operator will only test for null, not for empty ...
modified 19-Feb-19 7:15am.
|
|
|
|
|
RickZeeland wrote: if (name?.Length >= 3) However, this will return 'false when the input is a null string, or an empty string, or any string with 'Length < #3.
So, why do you say it tests only for 'null ?
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
Updated the answer
|
|
|
|
|
RickZeeland wrote: Updated the answer i know my eyesight is in deep trouble, but I can't see what changed
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
There's nothing negative about it, despite your "!".
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
A better name for the function is string.IsNotNullAndHasALengthGreaterThanZero - there, very positive and precise!
- I would love to change the world, but they won’t give me the source code.
|
|
|
|
|
Using your logic, that function name should be:
string.IsNotNullAndNotEmptyAndHasALengthGreaterThan2
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
You're right! Although perhaps it should be:
string.IsNotNullCommaNotEmptyAndHasALengthGreaterThanZero
...I'm not sure where the Length > 2 part you mentioned came in.
- I would love to change the world, but they won’t give me the source code.
|
|
|
|
|
I think it mostly depends on how the string identifies itself at any given moment - this is for all the snowflakes out ther (you know who you are).
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
How to swear in Latin[^]
What do you get when you cross a joke with a rhetorical question?
The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism.
Do questions with multiple question marks annoy you???
|
|
|
|
|
So useful!
I was still calling people coprolite. I guess this is much more advanced!
|
|
|
|
|
This would be much more interesting if it included a list of curses used by the Romans, rather than translations of modern curses.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
Even in Latin, not exactly KSS or appropriate for the lounge, IMO.
Latest Article - Slack-Chatting with you rPi
Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny
Artificial intelligence is the only remedy for natural stupidity. - CDP1802
|
|
|
|
|
"Leno = pimp"
Is that what he's up to these days...
|
|
|
|