Utility class
The StringUtils
utility class contains methods for a number of different purposes in relation to parsing or manipulating strings.
Parsing
.NET generally has a lot of great methods for parsing strings into other types, but these methods will fail if they see a format that they don't recognize.
ParseBoolean
For instance, there are a number of different ways to represent a boolean value as a string. The Parse
and TryParse
methods in .NET only knows about true
and false
as boolean values, but not values such as True
and False
or 1
and 0
. If the
In Skybrud.Essentials, the ParseBoolean
method will look for known variations of true
, but otherwise return false
if the value isn't recognized. No exceptions are thrown. Supported string values are true
, 1
, t
and on
(all case-insensitive).
ParseInt32
In a similar way to parsing booleans, you can parse a string value into an integer using the ParseInt32
method. If the parsing fails, 0
will be returned instead of the method throwing any exceptions.
// Returns "42"
int value1 = StringUtils.ParseInt32("42");
// Returns "0"
int value2 = StringUtils.ParseInt32("hello");
An overload of the methods also let's you specify a fallback - so if the parsing fails, it wil return your fallback value instead of 0
:
// Returns "123"
int value2 = StringUtils.ParseInt32("hello", 123);
ParseInt32Array
If you have a string with multiple integer values, you can use the ParseInt32Array
method for parsing the string value into an actual array of integers:
int[] value = StringUtils.ParseInt32Array("1,2,3,4");
The method will detect the ,
(comma),
(space), \r
(carriage return), \n
(newline) and \t
(tab) characters as separators by default, but you can also explicitly specify the separators to be used:
int[] value = StringUtils.ParseInt32Array("1¤2@3?4", '¤', '@', '?');
Any values that isn't recognized will be ignored, so again this method is safe to use without any risks of exceptions.