数据类型转换与字符串处理

本文讲解了C#的隐式转换,显示转换,parse,Tostring,Convert的转换。


        隐式转换

             当对简单的值类型进行转换时,如果是按照 Byte,short,int,long,float,double从左到右(从短到长)进行转换的时候,可以直接进行转换(隐式转换),不用做任何说明。简单的代码示例:

[csharp]  view plain copy print ?
  1. static void Main(string[] args)  
  2.         {  
  3.             int a = 10;  
  4.             long b = a;  
  5.             Console.Write("b的值为:"+b);  
  6.             Console.ReadKey();  
  7.               
  8.         }  

        显示转换

       依然是对值类型进行转换时,从长字节转换成短字节,直接转换的话,编译器会提示“无法将类型 *转换为类型*,存在一个显示转换”,这时需要进行强制转换(显示转换)。简单的代码示例:

[csharp]  view plain copy print ?
  1. static void Main(string[] args)  
  2.         {  
  3.             long a = 10;  
  4.             int b = (int)a;  
  5.             Console.Write("b的值为:"+b);  
  6.             Console.ReadKey();     
  7.         }  

             每种数据类型都存在自身的范围,例如byte类型的范围是0~255,int型的范围是0~65535,当int型转换成byte类型时,如果超出了自身的范围时会怎么处理呢?看下代码示例:
[csharp]  view plain copy print ?
  1. staticvoid Main(string[] args)  
  2.         {  
  3.             int a = 256;  
  4.             byte b = (byte)a;  
  5.             Console.Write("b的值为:"+b);  
  6.             Console.ReadKey();     
  7.         }  

      这段代码的运行结果 “b的值为0”,如果把a的值改为257,则b的值为1。结果是怎么来的,编译器会把256转换成对应的二进制,也就是100000000,当转换成byte类型时(8位二进制数),会长出8位的部分截掉,因此结果变成了0。

       

       toString()转换

      当把值类型转换成字符串类型时,可以直接调用值类型的方法toString()进行转换,另外toString还可以将结果转换成相应的进制形式,简单的代码示例:

[csharp]  view plain copy print ?
  1. <span style="font-size:18px;">staticvoid Main(string[] args)  
  2.         {  
  3.             //转换为对应的字符串类型  
  4.             int a = 256;  
  5.             string b =a.ToString();  
  6.             Console.Write("b的值为:"+b);  
  7.             Console.ReadKey();     
  8.         }  
  9.   
  10.  static void Main(string[] args)  
  11.         {  
  12.             //转换为十六进制形式(X可以换为小写,代表对应的a-f的大小写)  
  13.             int a = 256;  
  14.             string b =a.ToString("X");  
  15.             //输出结果为“b的值为100”  
  16.             Console.Write("b的值为:"+b);  
  17.             Console.ReadKey();     
  18.         }</span>  

       parse方法

      像int、long、float类型都有parse方法,可以将字符串转换为对应的数据类型,简单的代码实例:

[csharp]  view plain copy print ?
  1. <span style="font-size:18px;">staticvoid Main(string[] args)  
  2.         {  
  3.             int a = 256;  
  4.             string b ="256";  
  5.             if (int.Parse(b) ==a)  
  6.             {  
  7.                 Console.Write("a和b的值相等!");  
  8.                 Console.ReadKey();     
  9.             }  
  10.         }</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-
//       --







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值