您的位置:首页 > 编程语言 > C#

C#编程--字符串处理

2012-12-02 02:39 267 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: