c sharp 学习(二)

  今天继续c sharpe的基础笔记记录.

8.   函数重载

  函数重载在编程语言中是一个相当常用的概念,目的是使用户使用更方便.

  函数重载的基本形式是相同函数名,不同的函数参数(参数个数不同或者参数类型不同)

  注意:两个函数名相同,返回值不同,但参数形式相同也不能构成重载,编译都无法通过.函数重载主要是通过对多次参数形式定义一增强其通用性

9.   字符串知识

  a)字符串的长度

  对于字符串的定义string s = "hello";可以调用s.length来看字符串的长度.此处为5,有别于C和C++的6(包括了\0)

  b)字符串的读取

  string定义的字符串即相当于数组,对其的索引可对特定位进行读取

  如: string s = "hello";console.writeline(s[0]);//输出h

  c)字符串改变

  这一点需要需要注意,这样的语句是错误的: s[0]='a';

  事实上,在定义string s = "hello"时,开辟了一块内存并存放了数据hello,同时用 s 来指向,操作 s 即实现对hello的取值

  然而hello的定义后只能读,无法改变.若要对已定义字符串作一定程度的更改,有两种方法

    I.转换为char数组进行更改

      string s = "hello";

      char []chars=s.tochararray();

      chars[0]='a';

      string f = new string(chars);

      console.writeline(f);//实现输出aello

    2.对 s 进行更改

      string s = "hello";

      string s = "aello";

  但是不管上面进行怎样的操作,实际上hello还是hello,只不过是重新定义了"aello",而hello没有指向的变量而取不到了.一般操作运用 a 方法.

10.  字符串的应用

  此点比较重要就不列入第九点了.主要谈谈一些字符串函数

  a)ToLower():得到字符串的小写形式

   ToUpper():得到字符串的大写形式

   Trim()去掉字符串两端的空白

   s1.Equals(s2, StringComparison.OrdinalIgnoreCase),两个字符串进行比区分大小写的比较

  b)字符串的分割:

   string[] Split(params char[] separator):将字符串按照指定的分割符分割为字符串数组;

   string[] Split(char[] separator, StringSplitOptions options)将字符串按照指定的char分割符分割为字符串数组( options 取RemoveEmptyEntries的时候移除结果中的空白字符串);

   string[] Split(string[] separator, StringSplitOptions options)将字符串按照指定的string分割符分割为字符串数组。

  例如:1.string s = "aaa,bbb,ccc,fadsfas";

    string []strs=s1.split(',');

     foreach(string item in strs)

     {

      console.writeline(item);

     }//输出aaa bbb ccc fadsfas

   2.string s = "aaa,bb,,dd,eee";

    string []strs=s1.split(new char[]={','},StringSplitOptions options.RemoveEmptyEntries)

     foreach(string item in strs)

     {

      console.writeline(item);

     }//输出aaa bb dd eee

   c)字符串函数详解

    字符串替换:string Replace(string oldValue, string newValue)将字符串中的出现oldValue的地方替换为newValue。

    取子字符串:string Substring(int startIndex),取从位置startIndex开始一直到最后的子字符串;

    string Substring(int startIndex, int length),取从位置startIndex开始长度为length的子字符串,如果子字符串的长度不足length则报错。

    bool Contains(string value)判断字符串中是否含有子串value;
    bool StartsWith(string value)判断字符串是否以子串value开始

    bool EndsWith (string value)判断字符串是否以子串value结束;
    int IndexOf(string value):取子串value第一次出现的位置。

posted on 2011-08-03 14:55  Rick_w  阅读(1211)  评论(0编辑  收藏  举报

导航