您的位置:首页 > 移动开发 > Unity3D

蓝鸥Unity开发基础——字符串

2016-08-25 09:23 197 查看
蓝鸥Unity开发基础——字符串
一、字符串——string

String是引用类型,本质是一个char类型的数组

String是引用类型,本质是一个char类型的数组举例说明:

using System;

namespace Lesson10
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            //字符串string本质上是一个char类型的数组
//            char[] c=new char[3];
//            c[0]="A";
//            c[1]="B";

            string str = "ABCD";
            //可以使用下标索引字符串中的字符元素
            Console.WriteLine(str[2]);

            //字符类型
//            char c="A";
//            Console.WriteLine (c);

            //用加法运算符连接两个字符串
//            string s1 = "Hello,";
//            string s2 = "lanou!";
            //Console.WriteLine (s1+s2);
            //适用== 来对比是否完全相等

//            string s1 = "Hello,";
//            string s2 = "lanou!";
//            if (s1 == s2) {
                
//                Console.WriteLine ("文本内容完全相等");
//            } else {
//                Console.WriteLine ("文本内容完全不相等");
//            }

        }
    }
}

二、字符串方法和属性

 

using System;

namespace Lesson10
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            string str = "12/345/637/89";
            //检测字符串中是否包含指定的字符串;
            bool b=str.Contains("305");
            Console.WriteLine (b);
            //用来返回字
4000
符串中,首次出现指定字符的下标位置
            int i = str.IndexOf ("3");
            Console.WriteLine (i);
            //从指定下标位置,删除后面的字符串
            string ss= str.Remove (3);
            Console.WriteLine (ss);
            //删除字符串
            string s1= str.Remove (3,5);
            Console.WriteLine (s1);

            //替换指定的字符串或字符
            string s=str.Replace("3","X");
            Console.WriteLine (s);

            //分割字符串
            string [] strs=    str.Split(new char[]{'/'},4);
    
            foreach(string temps in strs ){
                
//                Console.WriteLine (temps);
                
            }
            //获取字符串
            string ts=str.Substring(4,3);
            Console.WriteLine (ts);

        }
    }
}

 


练习:“LanOuKeJi/Jinwuxing/Qinghe/Haidian/Beijing2016”

要求:将字符串中的每个单词分别单独输出在控制台上

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息