`
bcyy
  • 浏览: 1823786 次
文章分类
社区版块
存档分类
最新评论

C#字符串类的典型用法

 
阅读更多
using System;
namespace e1_4_11
{
    class Program
    {
        static void Main(string[] args)
        {
            //字符串搜索
            string s = "ABC科学";
            int j = s.IndexOf("科");
            Console.WriteLine("{0}", j);

            string s1 = "abc";
            string s2 = "abc";
            string s3 = "相同";
            //判断连个字符串是否相同
            if (s1 == s2)
                Console.WriteLine("{0}", s3);
            else
            {
                s3 = "不相同";
                Console.WriteLine("{0}", s3);
            }

            //判断字符串是否为空
            string s4 = "";
            string s5 = "不空";
            if (s4.Length == 0)
            {
                s5 = "空";
                Console.WriteLine("{0}", s5);
            }

            string s6 = "取子字符串";
            //从索引为2开始取2个字符,sb="字符",s内容不变
            string sb = s6.Substring(2, 2); //sb="字符"
            char sb1 = s6[0];
            Console.WriteLine(sb);
            Console.WriteLine(sb1);

            //删除字符串
            string sb2 = s6.Remove(0, 2); //从索引为0开始删除2个字符,sb="字符串",s6内容不变
            Console.WriteLine(sb2);

            //插入字符串
            string s7 = "计算机科学";
            string s8 = s7.Insert(3, "软件");
            Console.WriteLine(s8);

            //替换字符串
            string s9 = s7.Replace("计算机", "软件");
            Console.WriteLine(s9);

            //将字符串转换为字符数组
            char[] s10 = s7.ToCharArray(0, s7.Length);
            Console.WriteLine(s10);

            //转换为字符串
            int i = 9;
            string s11 = i.ToString();
            Console.WriteLine(s11);

            //大小写转换
            string st = "AaBbCc";
            string s12 = st.ToLower();
            string s13 = st.ToUpper();
            Console.WriteLine(s12);
            Console.WriteLine(s13);

            //删除所有空格
            string sk = "A   bc";
            Console.WriteLine(sk.Trim());
        }
    }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics