Click here to Skip to main content
15,891,431 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Code Project Blocking Add Blockers Pin
#realJSOP19-Feb-19 8:05
mve#realJSOP19-Feb-19 8:05 
GeneralRe: Code Project Blocking Add Blockers Pin
ZurdoDev19-Feb-19 8:09
professionalZurdoDev19-Feb-19 8:09 
GeneralRe: Code Project Blocking Add Blockers Pin
Pete O'Hanlon19-Feb-19 7:36
mvePete O'Hanlon19-Feb-19 7:36 
GeneralRe: Code Project Blocking Add Blockers Pin
Mycroft Holmes19-Feb-19 11:27
professionalMycroft Holmes19-Feb-19 11:27 
GeneralRe: Code Project Blocking Add Blockers Pin
Mark_Wallace19-Feb-19 7:50
Mark_Wallace19-Feb-19 7:50 
GeneralMessage Closed Pin
18-Feb-19 14:39
doobiester 18-Feb-19 14:39 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
PIEBALDconsult18-Feb-19 15:12
mvePIEBALDconsult18-Feb-19 15:12 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
BillWoodruff18-Feb-19 22:14
professionalBillWoodruff18-Feb-19 22:14 
I like to write little libraries for code re-use:
using System;
using System.Globalization;

namespace WHATEVER
{
    public static class StringExtensions
    {
        // holds result of last successful conversion to 'double
        private static double dbl;

        // holds result of last successful conversion to 'int
        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;

            // https://stackoverflow.com/a/2751597/133321
            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

GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
Richard Deeming25-Feb-19 8:10
mveRichard Deeming25-Feb-19 8:10 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
BillWoodruff25-Feb-19 20:40
professionalBillWoodruff25-Feb-19 20:40 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
Super Lloyd18-Feb-19 16:19
Super Lloyd18-Feb-19 16:19 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
the goat in your machine18-Feb-19 17:15
the goat in your machine18-Feb-19 17:15 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
doobiester 19-Feb-19 2:11
doobiester 19-Feb-19 2:11 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
OriginalGriff18-Feb-19 20:53
mveOriginalGriff18-Feb-19 20:53 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
Nelek18-Feb-19 23:51
protectorNelek18-Feb-19 23:51 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
kalberts18-Feb-19 21:02
kalberts18-Feb-19 21:02 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
RickZeeland18-Feb-19 21:49
mveRickZeeland18-Feb-19 21:49 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
BillWoodruff19-Feb-19 0:08
professionalBillWoodruff19-Feb-19 0:08 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
RickZeeland19-Feb-19 1:10
mveRickZeeland19-Feb-19 1:10 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
BillWoodruff19-Feb-19 15:38
professionalBillWoodruff19-Feb-19 15:38 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
Eddy Vluggen19-Feb-19 3:11
professionalEddy Vluggen19-Feb-19 3:11 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
  Forogar  19-Feb-19 3:41
professional  Forogar  19-Feb-19 3:41 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
BillWoodruff19-Feb-19 15:35
professionalBillWoodruff19-Feb-19 15:35 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
  Forogar  21-Feb-19 2:32
professional  Forogar  21-Feb-19 2:32 
GeneralRe: C# Improving string.IsNullOrEmpty readability Pin
#realJSOP19-Feb-19 8:10
mve#realJSOP19-Feb-19 8:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.