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
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#网络编程(异步传输字符串) - Part.3
- c#基础--字符串的处理_string类
- C#编程入门_文件处理IO_17
- C#网络编程_同步传输字符串
- C# 字符串,数组,日期常遇到的处理方法(持续更新)
- C# 字符串,数组,日期常遇到的处理方法(持续更新)
- C#字符串处理机制
- [连载]C#程序设计(07)--- C#核心编程-5 --- 数组和字符串
- 有一字符串,包含数字与字母,编程去除数字。要求:1要求在原字符串中操作 2、使用指针处理
- C#常用字符串处理
- 编程学习笔记4--字符串处理
- C#网络编程(异步传输字符串) - Part.3
- 第四课输入时的细节处理——C#计算器编程教学
- (原创)c#学习笔记05--变量的更多内容03--字符串的处理
- 在进行C#编程时候,有的时候我们需要判断一个字符串是否是数字字符串,我们可以通过以下两种方法来实现。 【方法一】:使用 try{} catch{} 语句。 我们可以在try语句块中试图
- C#字符串处理类
- C# + SQL中更新单个数据时的查询字符串处理
- c#编程指南(十) 平台调用P-INVOKE完全掌握, 字符串和指针