本文讲解了C#的隐式转换,显示转换,parse,Tostring,Convert的转换。
隐式转换
当对简单的值类型进行转换时,如果是按照 Byte,short,int,long,float,double从左到右(从短到长)进行转换的时候,可以直接进行转换(隐式转换),不用做任何说明。简单的代码示例:
- static void Main(string[] args)
- {
- int a = 10;
- long b = a;
- Console.Write("b的值为:"+b);
- Console.ReadKey();
- }
显示转换
依然是对值类型进行转换时,从长字节转换成短字节,直接转换的话,编译器会提示“无法将类型 *转换为类型*,存在一个显示转换”,这时需要进行强制转换(显示转换)。简单的代码示例:
- static void Main(string[] args)
- {
- long a = 10;
- int b = (int)a;
- Console.Write("b的值为:"+b);
- Console.ReadKey();
- }
每种数据类型都存在自身的范围,例如byte类型的范围是0~255,int型的范围是0~65535,当int型转换成byte类型时,如果超出了自身的范围时会怎么处理呢?看下代码示例:
- staticvoid Main(string[] args)
- {
- int a = 256;
- byte b = (byte)a;
- Console.Write("b的值为:"+b);
- Console.ReadKey();
- }
这段代码的运行结果 “b的值为0”,如果把a的值改为257,则b的值为1。结果是怎么来的,编译器会把256转换成对应的二进制,也就是100000000,当转换成byte类型时(8位二进制数),会长出8位的部分截掉,因此结果变成了0。
toString()转换
当把值类型转换成字符串类型时,可以直接调用值类型的方法toString()进行转换,另外toString还可以将结果转换成相应的进制形式,简单的代码示例:
- <span style="font-size:18px;">staticvoid Main(string[] args)
- {
- //转换为对应的字符串类型
- int a = 256;
- string b =a.ToString();
- Console.Write("b的值为:"+b);
- Console.ReadKey();
- }
- static void Main(string[] args)
- {
- //转换为十六进制形式(X可以换为小写,代表对应的a-f的大小写)
- int a = 256;
- string b =a.ToString("X");
- //输出结果为“b的值为100”
- Console.Write("b的值为:"+b);
- Console.ReadKey();
- }</span>
parse方法
像int、long、float类型都有parse方法,可以将字符串转换为对应的数据类型,简单的代码实例:
- <span style="font-size:18px;">staticvoid Main(string[] args)
- {
- int a = 256;
- string b ="256";
- if (int.Parse(b) ==a)
- {
- Console.Write("a和b的值相等!");
- Console.ReadKey();
- }
- }</span>
Convert类
Convert有很多的转换数据类型的方法,它将继承自Object类型的对象转换为制定的类型,即Convert.toInt32()(还有对应其他类型的转换)。另外的一种方法:Convert.ChangeType(Object,Type),这种方法在涉及到泛型时会显现出优势。
处理字符串的方法:
string.ToCharArray(); //将字符串输出为字符数组
string.ToLower(); //将字符串输出为大写字母
string.ToUpper(); //将字符串输出为小写字母
string.Trim(); //将字符串前后的空格删除
string.PadLeft(Int32); //右对齐此实例中的字符,在左边用空格填充以达到指定的总长度。
string.PadLeft (Int32, Char); //右对齐此实例中的字符,在左边用指定的 Unicode 字符填充以达到指定的总长度。
string.PadRight(Int32); //同理
string.split(char[]); //返回的字符串数组包含此实例中的子字符串
string.split(char[],Int32);
split方法的两个示例:
using System; public class SplitTest { public static void Main() { string words = "This is a list of words, with: a bit of punctuation" + "\tand a tab character."; string [] split = words.Split(new Char [] {' ', ',', '.', ':', '\t' }); foreach (string s in split) { if (s.Trim() != "") Console.WriteLine(s); } } } // The example displays the following output to the console: // This // is // a // list // of // words // with // a // bit // of // punctuation // and // a // tab // character
using System; public class StringSplit2 { public static void Main() { string delimStr = " ,.:"; char [] delimiter = delimStr.ToCharArray(); string words = "one two,three:four."; string [] split = null; Console.WriteLine("The delimiters are -{0}-", delimStr); for (int x = 1; x <= 5; x++) { split = words.Split(delimiter, x); Console.WriteLine("\ncount = {0,2} ..............", x); foreach (string s in split) { Console.WriteLine("-{0}-", s); } } } } // The example displays the following output: // The delimiters are - ,.:- // count = 1 .............. // -one two,three:four.- // count = 2 .............. // -one- // -two,three:four.- // count = 3 .............. // -one- // -two- // -three:four.- // count = 4 .............. // -one- // -two- // -three- // -four.- // count = 5 .............. // -one- // -two- // -three- // -four- // --