字符串转换 [英] String casts

查看:100
本文介绍了字符串转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么有这样的方法可以转换为.net中的字符串?我看到的方式是.ToString,Convert.ToString()和(string)。有什么不同。

Why is there so may ways to convert to a string in .net? The ways I have seen are .ToString, Convert.ToString() and (string). What is the Difference.

推荐答案

Convert.ToString(obj)



将指定的值转换为其等效的String表示形式。如果指定的值为 null ,将返回 String.Empty

返回表示当前对象的字符串。此方法返回一个对人敏感的,对文化敏感的字符串。例如,对于其值为零的Double类的实例,根据当前UI文化,Double.ToString的实现可能返回0.00或0,00。默认实现返回对象类型的完全限定名称。

Returns a String that represents the current Object. This method returns a human-readable string that is culture-sensitive. For example, for an instance of the Double class whose value is zero, the implementation of Double.ToString might return "0.00" or "0,00" depending on the current UI culture. The default implementation returns the fully qualified name of the type of the Object.

此方法可以在派生类中重载,以返回对该类型有意义的值。例如,基本数据类型(例如Int32)实现ToString,以便它返回对象表示的值的字符串形式。派生类需要更多的控制字符串的格式比ToString提供必须实现IFormattable,其ToString方法使用当前线程的CurrentCulture属性。

This method can be overridden in a derived class to return values that are meaningful for that type. For example, the base data types, such as Int32, implement ToString so that it returns the string form of the value that the object represents. Derived classes that require more control over the formatting of strings than ToString provides must implement IFormattable, whose ToString method uses the current thread's CurrentCulture property.

操作,而不是函数调用。如果您确定对象的类型为字符串,或者它具有可以将其转换为字符串的隐式或显式运算符,请使用它。将返回 null 如果对象是 null AND类型String或类型实现自定义转换为字符串运算符。查看示例。

It's a cast operation, not a function call. Use it if you're sure that the object is of type string OR it has an implicit or explicit operator that can convert it to a string. Will return null if the object is null AND of type String or of type which implements custom cast to string operator. See examples.

安全铸造操作。与上面相同,但是如果转换操作失败,而不是抛出异常,它将返回 null

Safe cast operation. Same as above, but instead of throwing an exception it will return null if cast operation fails.

提示:不要忘记使用CultureInfo with obj.ToString() Convert.ToString(obj)

Hint: Don't forget to use CultureInfo with obj.ToString() and Convert.ToString(obj)

12345.6D.ToString(CultureInfo.InvariantCulture);          // returns 12345.6
12345.6D.ToString(CultureInfo.GetCultureInfo("de-DE"));   // returns 12345,6
Convert.ToString(12345.6D, CultureInfo.InvariantCulture); // returns 12345.6
Convert.ToString(12345.6D, CultureInfo.GetCultureInfo("de-DE"));  // 12345,6
Convert.ToString(test);  // String.Empty, "test" is null and it's type
                         // doesn't implement explicit cast to string oper.
Convert.ToString(null);  // null
(string) null;           // null
(string) test;           // wont't compile, "test" is not a string and
                         // doesn't implement custom cast to string operator
(string) test;           // most likely NullReferenceException,
                         // "test" is not a string,
                         // implements custom cast operator but is null
(string) test;           // some value, "test" is not a string,
                         // implements custom cast to string operator
null as string;          // null

以下是自定义转换运算符的示例:

Here is an example of custom cast operator:

public class Test
{
    public static implicit operator string(Test v)
    {
        return "test";
    }
}

这篇关于字符串转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆