Share via


StringExtensions Class

Provides utility methods for converting string values to other data types.

Inheritance Hierarchy

System.Object
  System.Web.WebPages.StringExtensions

Namespace:  System.Web.WebPages
Assembly:  System.Web.WebPages (in System.Web.WebPages.dll)

Syntax

'Declaration
<ExtensionAttribute> _
Public NotInheritable Class StringExtensions
'Usage
public static class StringExtensions
[ExtensionAttribute]
public ref class StringExtensions abstract sealed
[<AbstractClassAttribute>]
[<SealedAttribute>]
type StringExtensions =  class end
public final class StringExtensions

The StringExtensions type exposes the following members.

Methods

  Name Description
Public methodStatic member As<TValue>(String) Converts a string to a strongly typed value of the specified data type.
Public methodStatic member As<TValue>(String, TValue) Converts a string to the specified data type and specifies a default value.
Public methodStatic member AsBool(String) Converts a string to a Boolean (true/false) value.
Public methodStatic member AsBool(String, Boolean) Converts a string to a Boolean (true/false) value and specifies a default value.
Public methodStatic member AsDateTime(String) Converts a string to a DateTime value.
Public methodStatic member AsDateTime(String, DateTime) Converts a string to a DateTime value and specifies a default value.
Public methodStatic member AsDecimal(String) Converts a string to a Decimal number.
Public methodStatic member AsDecimal(String, Decimal) Converts a string to a Decimal number and specifies a default value.
Public methodStatic member AsFloat(String) Converts a string to a Single number.
Public methodStatic member AsFloat(String, Single) Converts a string to a Single number and specifies a default value.
Public methodStatic member AsInt(String) Converts a string to an integer.
Public methodStatic member AsInt(String, Int32) Converts a string to an integer and specifies a default value.
Public methodStatic member Is<TValue> Checks whether a string can be converted to the specified data type.
Public methodStatic member IsBool Checks whether a string can be converted to the Boolean (true/false) type.
Public methodStatic member IsDateTime Checks whether a string can be converted to the DateTime type.
Public methodStatic member IsDecimal Checks whether a string can be converted to the Decimal type.
Public methodStatic member IsEmpty Checks whether a string value is null reference (Nothing in Visual Basic) or empty.
Public methodStatic member IsFloat Checks whether a string can be converted to the Single type.
Public methodStatic member IsInt Checks whether a string can be converted to an integer.

Top

Remarks

Web pages often accept user input as strings. A typical example is a page that contains an HTML form for user account information. The form might contain fields for entering text information, dates, numbers, true or false values, and currency values. When a user submits the data to a web server, the values are sent over HTTP as strings. When your server code handles the posted data, you often want to convert string values to another data type. For example, it is more effective to compare, sort, and work with dates and numbers programmatically if they are in an appropriate date or numeric data type, instead of being in string format.

The StringExtensions class contains two categories of methods for converting strings to other data types:

  • The As methods. These method names all begin with As and are based on the As() method. They convert string values to a specified type. For example, the AsInt() method converts a string value of "31" to an actual integer-typed value of 31. Similarly, the AsBool() method converts a string value of "true" to the Boolean-typed value of true.

  • The Is methods. These method names all begin with Is and are based on the Is<TValue>(String) method. They test whether a string value can be converted to the specified type. For example, the IsBool(String) method returns true if the string value passed to the method is "true" or "false". The IsDateTime(String) method returns true if the string value (such as a date string value of "12/25/2010") passed to the method can be converted to a T:System.DateTime value.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

System.Web.WebPages Namespace