Wanzyee Studio
StringExtension

Extension methods for string. More...

Static Public Member Functions

static T To< T > (this string text, T defaultValue=default, bool exception=false)
 Syntactic sugar to convert a string to the specified type. More...
 
static object To (this string text, Type type, object defaultValue=null, bool exception=false)
 Syntactic sugar to convert a string to the specified type. More...
 
static float[] ExtractFloats (this string text)
 Extract all the float from specified string. More...
 
static byte[] ExtractBytes (this string text)
 Extract all the byte from specified string. More...
 

Detailed Description

Extension methods for string.

Member Function Documentation

static T To< T > ( this string  text,
defaultValue = default,
bool  exception = false 
)
static

Syntactic sugar to convert a string to the specified type.

Use methods below for generic conversion:

  1. TypeConverter.ConvertFromString(), you may make custom converter to support in need.
  2. Static Parse() method declared by the value type.
  3. JsonUtility.FromJson() as fallback.
Template Parameters
TThe value type.
Parameters
textText.
defaultValueThe default value.
exceptionIf convert failed, throw an exception or return the default value.
Returns
The value.
static object To ( this string  text,
Type  type,
object  defaultValue = null,
bool  exception = false 
)
static

Syntactic sugar to convert a string to the specified type.

Use methods below for generic conversion:

  1. TypeConverter.ConvertFromString(), you may make custom converter to support in need.
  2. Static Parse() method declared by the value type.
  3. JsonUtility.FromJson() as fallback.
Parameters
textText.
typeThe value type.
defaultValueThe default value.
exceptionIf convert failed, throw an exception or return the default value.
Returns
The value.
static float [] ExtractFloats ( this string  text)
static

Extract all the float from specified string.

E.g., input "12.3x, -4.5e2)&1.6e-1#N.047" to result [12.3, -450, 0.16, 47].

Parameters
textText.
Returns
The floats.
static byte [] ExtractBytes ( this string  text)
static

Extract all the byte from specified string.

Any invalid char within is removed directly, and parse the remain char sequence. E.g., input "{05, Ad}+/720fR*bYTes.C" to result [05-AD-72-0F-BE].

Parameters
textText.
Returns
The bytes.