Last active
July 17, 2019 15:56
-
-
Save grhbit/d5188312732e9b9dd868 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace luunate | |
{ | |
public static class StringUtils | |
{ | |
public static string Join(this IEnumerable<string> values, string separator = " ") { | |
return string.Join (separator.IfNull(" "), values); | |
} | |
public static string Form(this string format, params object[] args) { | |
return string.Format(format.IfNull(""), args); | |
} | |
public static string IfNull(this string val, string defaultStr) { | |
if (val == null) { | |
return defaultStr; | |
} | |
return val; | |
} | |
public static string IfNullOrEmpty(this string val, string defaultStr) { | |
if (val.IfNull("").Length == 0) { | |
return defaultStr; | |
} | |
return val; | |
} | |
public static float ToFloat(this string val) { | |
float result; | |
result = float.Parse(val.IfNull("")); | |
return result; | |
} | |
public static float ToFloat(this string val, float defaultStr) { | |
float result; | |
if (!float.TryParse(val.IfNullOrEmpty(""), out result)) { | |
result = defaultStr; | |
} | |
return result; | |
} | |
public static int? ToInt(this string val) { | |
int result; | |
if (!int.TryParse (val.IfNull (""), out result)) { | |
return null; | |
} | |
return result; | |
} | |
public static int ToInt(this string val, int defaultStr) { | |
int result; | |
if (!int.TryParse(val.IfNullOrEmpty(""), out result)) { | |
result = defaultStr; | |
} | |
return result; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment