C#編程--字符串處理

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
       
        static void Main(string[] args)
        {
            //字符串處理
            string s = "hello";

            //1,得到字符串的長度
            Console.WriteLine("字符串的長度"+s.Length);

            //2,利用數組操作字符串 只能讀不能寫 字符串一旦聲明 ,不能改變
            Console.WriteLine("第一個字符" + s[0]);

            //3,轉換爲字符
            char[] arr = s.ToCharArray();
            arr[0] = 'A';

            //4,利用數組構造字符串
            s = new string(arr);
            Console.WriteLine(s);

            //5,字符串轉換成小寫
            String t = s.ToLower();
            Console.WriteLine(t);

            //6,轉換成大寫
            t = s.ToUpper();
            Console.WriteLine(t);

            //7,去掉空白兩邊
            Console.WriteLine(s.Trim());

            //8,字符串的比較
            Console.WriteLine("aello".Equals(s));
            Console.WriteLine("aello".Equals(s, StringComparison.OrdinalIgnoreCase)); //忽略大小寫的比較
            Console.WriteLine("Aello" == s); //==區分大小寫的比較

            //9,分割字符串
            s = "aaa,bbb,ccc,,   ddd|sdf|dddd|ddd";
            string[] strArr = s.Split(',','|');
            foreach (string temp in strArr)
            {
                Console.WriteLine(temp);
            }
            //             也可以換成字符串的數組
            strArr = s.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries) ;//移除空的元素
            foreach(string strtemp in strArr)
            {
                Console.WriteLine(strtemp);
            }

            //10.字符串替換
            s = "11111";
            string temp2 = s.Replace("11", "22");
            Console.WriteLine(temp2);

            //11,字符串截取
            s = "1234567890";
            Console.WriteLine(s.Substring(2));  //從索引2號開始截取
            Console.WriteLine(s.Substring(2,4));  //                           ,截取4個字符

            //12,判斷是否含有子川
            Console.WriteLine(s.Contains("345"));

            //13,判斷字符串是否以什麼開頭
            Console.WriteLine(s.StartsWith("123"));
            //s.EndsWith();



            Console.ReadKey();
        }
    }
}
 

本文出自 “Kenan_ITBlog” 博客,請務必保留此出處http://soukenan.blog.51cto.com/5130995/1076244

發佈了115 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章