导航

string 类型 学习

Posted on 2014-08-11 19:40  SN_XIA  阅读(159)  评论(0编辑  收藏  举报

参考网址:

http://terrylee.cnblogs.com/archive/2005/12/26/304876.html

http://www.cnblogs.com/dongzhiquan/archive/2011/11/05/2237088.html

http://www.cnblogs.com/ydpvictor/archive/2012/11/16/2774158.html

http://blog.csdn.net/jenny0107/article/details/1131381

http://kb.cnblogs.com/page/50562/

http://kb.cnblogs.com/page/75290/

http://www.cnblogs.com/artech/archive/2007/05/31/765773.html

    string类型:字符串,在很多地方都需要使用,但是我对string类型还是有很多的不清楚,除去一些常见的方法,对string类型没有一定的深度了解。下面是对它的学习汇总。

1、关于string的认识

  string类型 在堆中被定义 可改变对象 任何对string 类型的修改都是创建新的地址去存储信息  同时相同的字符串,所指向的内存地址相同(string 驻留)

  如下:大家都知道引用类型作比较时,既要比较值,也要比较地址,而string类型,由于其在内存中的驻留,相同的字符串指向的都是相同的地址(我是这么理解的)。

 1 static void Main(string[] args)
 2         {
 3 
 4             string a = "ab";
 5             string a1 = "ab";
 6             string b = "a";//对b的修改,会创建新的地址去存储信息
 7             b = b + "b";
 8             if (a == a1)
 9                 Console.WriteLine("string value equal");
10             if ((object)a == (object)a1)
11                 Console.WriteLine("object1 value equal");
12             if ((object)a == (object)b)
13                 Console.WriteLine("object value equal");//除非对b进行字符串的驻留String.Intern()
14         }

2、string常用的方法、属性

字段 :      返回获取字符串的长度:length;

方法 :       指定位置的字符:直接用对象名[位置-1],string类型char的集合

             文本大写表示ToUpper()

文本小写表示ToLower()

获取子字符串 startIndex:开始位置 。            length :子字符串长度

public string Substring(int startIndex);      

public string Substring(int startIndex, int length)

字符串实例的开头是否与指定的字符串匹配

public bool StartsWith(string value); 大小写有关

public bool StartsWith(string value, bool ignoreCase, CultureInfo culture); 可设置是否与大小写有关

 

分割字符串

public string[] Split(params char[] separator);//单个单个的效果好一点。

public string[] Split(string[] separator, StringSplitOptions options);//options决定是否返回空的子字符串,options枚举类型。

 

返回一个新字符串,其中当前实例中出现的所有指定字符串都替换为另一个指定的字符串

                   public string Replace(string oldValue, string newValue);

返回一个新字符串,其中此实例中出现的所有指定 Unicode 字符都替换为另一个指定的 Unicode 字符。

                   public string Replace(char oldChar, char newChar);

从此实例中的指定位置开始删除指定数目的字符。

                   public string Remove(int startIndex, int count);

删除此字符串中从指定位置到最后位置的所有字符。

                   public string Remove(int startIndex);

指示指定的字符串是 null 还是 System.String.Empty 字符串。

                   public static bool IsNullOrEmpty(string value);

在此实例中的指定索引位置插入一个指定的 System.String 实例。

                   public string Insert(int startIndex, string value);

返回类 System.String 的 System.TypeCode。

                   public TypeCode GetTypeCode();

将指定字符串中的一个或多个格式项替换为指定对象的字符串表示形式。

                   public static string Format(string format, object arg0);

返回一个值,该值指示指定的 System.String 对象是否出现在此字符串中。

                   public bool Contains(string value);

连接字符串

                   Concat

3、自我学习观点

  对于string类型,看了一些网页的内容,对自己来说真的是很有帮助。但学习的过程中,还需要进一步的学习,在查考的网站当中,有一些东西自己还是看不懂的,这方面需要自己进一步的学习,恩,不断的学习,不断的复习。