C#编程--字符串处理
2013-04-01 16:14
357 查看
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
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
相关文章推荐
- C#编程--字符串处理
- C#字符串处理
- C#字符串处理
- Shell脚本编程——字符串变量处理
- C#调用易语言字符串处理
- 串的简单处理 在实际的开发工作中,对字符串的处理是最常见的编程任务。 本题目即是要求程序对用户输入的串进行处理。
- c#序列化json字符串及处理
- UVa 10340 All in All (字符串处理&编程技巧)
- C#字符串处理(@)
- C#中字符串处理的一些自定义方法
- C#字符串处理
- 使用C#程序处理PowerPoint文件中的字符串
- C# UDP服务编程,数据处理流程设计(抛砖引玉)
- C语言编程,对字符串进行处理
- C#笔记5——C#常见字符串处理方法
- C# 实例练习——字符串处理(第一天)
- C#:从字符串反转看字符串处理
- c#解析json字符串处理(最清晰易懂的方法)
- c#(asp.net)接收字符串(多条json)处理
- C#字符串处理插入(Insert)函数