字符串类型转换 [英] String casts

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

问题描述

为什么会出现这样可能的方法来转换为字符串在.net中?我所看到的方法是的ToString,Convert.ToString()和(串)。区别是什么。

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.

推荐答案

将指定的值转换为其等效字符串重新presentation。将返回的String.Empty 如果指定的值

Convert.ToString(obj)

Converts the specified value to its equivalent String representation. Will return String.Empty if specified value is null.

返回重新presents当前对象的字符串。这个方法返回一个人类可读的字符串,它是文化敏感。例如,对于双类,其值为零的实例,Double.ToString的实施可能会返回0.00或0,00取决于当前的UI文化。默认实现返回对象的类型的完全限定名。

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使其返回值的字符串形式的对象重新presents。派生类需要更多的控制字符串格式比的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.

这是一个转换操作,而不是一个函数调用。使用它,如果你确信对象是字符串类型,或者它有一个隐含的或明确的运营商,可以将其转换为字符串。将返回如果对象为空字符串类型或实现自定义转换为字符串运算符类型。见的例子。

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.

安全转换操作。同上,但不是抛出一个异常,将返回如果转换操作失败。

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


提示:不要忘了使用的CultureInfo与 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天全站免登陆