---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

黑马程序员--学习笔记--String类与字符串处理技术

Posted on 2012-11-24 18:34  xiulan  阅读(161)  评论(0)    收藏  举报

一、            String类

关键字string与int、double等数据类型一样,它也是数据类型,只是是字符串数据类型,不一样的地方是int、double等是结构体的数据类型,而string比较特殊是个类!通常当string表示类时,我们一般都把它的s大写,即写为String,而当是关键字时,把s小写,即string,以更好地区别它充当的角色。

二、            字符串中字符的访问

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace 字符串的访问

{

    class Program

    {

        static void Main(string[] args)

        {

            string str = "hello,world";

            //因为字符串可以看做一个char的只读数组,所以可以用下标来对其访问

            Console.WriteLine(str[4]);

            Console.ReadKey();

        }

    }

}

结果:

 

三、            字符串处理技术

1、             将字符串中的变量所指向的值改变的办法

(1)、字符串的特性

1、 c#中字符串有一个重要的特性:不可改变性,字符串一旦生命就不再可以改变,所以只能通过索引来读取指定位置的char,不能对指定位置的char进行修改

所以string可以看做是char的只读数组

2、 如果要对char进行修改,那么就必须创建一个新的字符串,用s.ToCharArray()方法得到字符串的char数组,对数组进行修改后,调用new string(char[])这个构造函数来创建char数组的字符串。

3、一旦字符串被创建,那么char数组的修改也不会造成字符串的变化,这是字符串的特殊之处,这里就要清楚的区分字符串变量名和变量指向的值,程序中可以有很多字符串,然后由字符串变量指向它们,变量可以指向其他的字符串,但是字符串本身没有变化,字符串不可改变性指的是内存中的字符串不可变,而不是变量所指向的值不变。

 

(2)字符串元素改变的代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace tochar字符串

{

    class Program

    {

        static void Main(string[] args)

        {

            string s = "hello world";

            char[] chars = s.ToCharArray();//用s.ToCharArray()方法得到字符串的char数组

            chars[1] = 'i';//将i赋给下标为1的char数组元素

            s = new string(chars);//对数组进行修改后,调用new string(char[])这个构造函数来创建char数组的字符串。

            Console.WriteLine(s);//将字符串s这个变量所指向的新创建的字符串输出

            Console.ReadKey();

        }

    }

}

2、             字符串大小写的转换

(1)转换和比较的方法

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

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

3、由于字符串的不可改变性,所以这些函数都不会直接改变字符串的内容,而是把修改后的字符串通过函数返回值的形式返回。即s=s. ToUpper()

4、比较两个字符串的两种方法

(1)s1==s2

(2) s1.Equals(s2, StringComparison.OrdinalIgnoreCase))

(2)字符串转换大小写和比较两个字符串的代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace 字符串

{

    class Program

    {

        static void Main(string[] args)

        {

            string scoreA="java";

            string scoreB = "JAVA";

//由于字符串的不可改变性,字符串调用的方法,处理后的结果生成了一个全新的字符串,会作为返回值返回,要定义变量接收改变的值

            //将转换为小写的scoreA的值通过函数返回值的形式返回给scoreA

            scoreA = scoreA.ToLower();

            //将转换为小写的scoreB的值通过函数返回值的形式返回给scoreB

            scoreB = scoreB.ToLower();

            if (scoreA == scoreB)

            //Equals方法的Equals(string, StringComparison.OrdinalIgnoreCase))

                //if(scoreA.Equals(scoreB, StringComparison.OrdinalIgnoreCase))

            {

                Console.WriteLine("我们喜欢的课程一样");

            }

            else

            {

                Console.WriteLine("我们喜欢的课程不一样");

            }

            Console.ReadKey();

        }

    }

}

 

 

3、             字符串的分割

(1)           分割的方法

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

2、 string[]Split(char[]separator, StringSplitOptions.RemoveEmptyEntries)options取RemoveEmptyEntries移除结果中的空白字符串

(2)           分割的代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace spiltff

{

    class Program

    {

        static void Main(string[] args)

        {

            string str = "how are you?fine,thank you!";

            char[] removechar = { ' ', '?', '!', ',' };

            string[] words = str.Split(removechar, StringSplitOptions.RemoveEmptyEntries);

            Console.WriteLine("上句话中共有{0}个单词", words.Length);

            Console.WriteLine("这几个单词分别为:");

            for (int i = 0; i < words.Length; i++)

            {

                Console.Write(words[i]);

            }

            Console.ReadKey();

        }

    }

}

 

(3)分割日期习题

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace spiltff

{

    class Program

    {

        static void Main(string[] args)

        {

            //从日期字符串(2011-07-15)中分析出年、月、日:2011年07月15日

            string date = "2011-07-15";

            string[] sp = date.Split(new char[] {'-','/'},StringSplitOptions.RemoveEmptyEntries);

            Console.WriteLine("你刚刚输入的时间为:{0}年{1}月{2}日",sp[0],sp[1],sp[2]);

            Console.ReadKey();

        }

    }

}

 

4、字符串的替换

(1)替换的方法

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

 

(2)替换的代码举例

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace replace替换方法

{

    class Program

    {

        static void Main(string[] args)

        {

            string date = "2011/07/15";

            string d=date.Replace("/","-");//由于字符串的不可改变性,定义一个新的变量来接收replace函数改变字符串的返回值

            Console.WriteLine(d);//输出replace的返回值

            Console.ReadKey();

        }

    }

}

5、取子字符串

(1)取子字符串的方法

1、String Substring(int starindex),取从位置starindex开始一直到最后的子字符串

2、string Substring(int startindex ,int length),取从位置starindex开始长度的length的子字符串,如果子字符串的长度不足length则报错

 

(2)用方法1举例

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace substring取子字符串

{

    class Program

    {

        static void Main(string[] args)

        {

 

            string str = "hello,world";

            string result = str.Substring(6);

            Console.WriteLine(result);

            Console.ReadKey();

        }

    }

}

结果:

(3)、用方法2举例

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace substring取子字符串

{

    class Program

    {

        static void Main(string[] args)

        {

            string str = "hello,world";

            string result = str.Substring(6,2);

            Console.WriteLine(result);

            Console.ReadKey();

        }

    }

}

结果:

 

6、取子字符串第一次出现的位置

(1)取位置的方法

Int IndexOf(string value)返回子字符串或字符串第一次出现的索引位置(从0开始),如果没有找到子字符串,则返回-1

(2)返回找到字符串的位置的代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace indexof返回位置

{

    class Program

    {

        static void Main(string[] args)

        {

            string str = "hello,world";

            Console.WriteLine(str.IndexOf("o"));

            Console.ReadKey();

        }

    }

}

结果:

 

(3)当指定从哪个下标时,输出的子字符索引

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace indexof返回位置

{

    class Program

    {

        static void Main(string[] args)

        {

            string str = "hello,world";

            Console.WriteLine(str.IndexOf("o",5));//由于第一个o出现的位置索引是4,所以要想输出第二个o出现的位置,就要从5开始

            Console.ReadKey();

        }

    }

}

结果:

 

4、当找不到位置时后返回的值

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace indexof返回位置

{

    class Program

    {

        static void Main(string[] args)

        {

            string str = "hello,world";

            Console.WriteLine(str.IndexOf("g"));

            Console.ReadKey();

        }

    }

}

结果:

7、字符串是否包含子字符串

(1)找到包含字符串的方法

Bool contains(string value)判断字符串中是否含有子串value

(2)包含代码举例

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace contains子字符串的包含查询

{

    class Program

    {

        static void Main(string[] args)

        {

            string[] words = {"自卑","自负","凶神恶煞"};

            Console.WriteLine("请输入你想说的一句话");

            string hua = Console.ReadLine();

            int i;

            for (i = 0; i < words.Length; i++)//遍历用户输入的话,看是否包含words里的字符

            {

                if (hua.Contains(words[i]))

                {

                    break;

                }

            }

            if (i < words.Length)//如果程序是不是由于i++后的值不符合条件跳出的,就说明是包含了非法字符,是通过break跳出的

            {

                Console.WriteLine("你输入的内容中有非法字符,不能显示");

            }

            else

            {

                Console.WriteLine("你刚输入的是{0},已发表成功",hua);

            }

        }

    }

}

正常输入结果:

输入不允许输入的字符后的结果:

当包含了非法字符后,让其代替的为***的代码,即一段程序中既包含contains又包含replace

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace contains子字符串的包含查询

{

    class Program

    {

        static void Main(string[] args)

        {

            string[] words = {"自卑","自负","凶神恶煞"};

            Console.WriteLine("请输入你想说的一句话");

            string hua = Console.ReadLine();

            int i;

            for (i = 0; i < words.Length; i++)//遍历用户输入的话,看是否包含words里的字符

            {

                if (hua.Contains(words[i]))

                {

                    hua = hua.Replace(words[i],"***");  

                }

            }

            Console.WriteLine("你刚输入的是{0},已发表成功", hua);

            Console.ReadKey();

        }

    }

}

结果:

 

8、字符串的starwith和endwith用法

(1)两个用法的作用

1、Starwith的用法是:如果字符串以指定子字符串或指定字符开始,则返回真,否则返回假

2、endwith的用法是:如果字符串以指定子字符串或指定字符结束,则返回真,否则返回假

 

(2)两者的用法举例

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace starwith的用法

{

    class Program

    {

        static void Main(string[] args)

        {

            string s = "hello,world!";

             bool str=s.StartsWith("h");

             Console.WriteLine(str);

             Console.ReadKey();

        }

    }

}

结果:

Endwith的用法代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace starwith的用法

{

    class Program

    {

        static void Main(string[] args)

        {

            string s = "hello,world!";

            bool str = s.EndsWith("wo!");

             Console.WriteLine(str);

             Console.ReadKey();

        }

    }

}

结果:

 

四、字符串的小知识

 

1、 字符串中的链接+,两边只要有一个是字符串类型,另一个也会被自动转换成字符串类型

2、 一切类型都可以调用ToString()方法转换成字符串类型

3、  string s = string.Empty():一个指向空字符串变量,s不占用内存空间,s变量可以使用,比如s.Length

string s = null;一个指向“\0”的空字符串,占用一个字节存储空间,因为转义符\0代表字符串结束,s变量可以使用,比如s.Length

string s = "";一个不指向任何一个存储空间的变量,s变量不可以使用

 

---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------

详细请查看:http://edu.csdn.net/heima/

---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------

详细请查看:http://edu.csdn.net/heima/