C#学习:字符串函数详解
来源:互联网 发布:reflections java 编辑:程序博客网 时间:2024/06/12 22:27
一、字符串分割:Split
练习使用Split对字符串进行分割。
Split使用方法(1):string[] strs1 = s1.Split('分割符');
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 字符串分割{ class Program { static void Main(string[] args) { string s1 = "aaa,bbb.ccc\\ddd"; string[] strs = s1.Split(',','.','\\'); foreach (string item in strs) { Console.WriteLine(item); } Console.ReadKey(); } }}
Split可以使用可变参数,即可以规定多个分隔符。(注意转义字符\作为分隔符时的情况)
执行结果:
字符串根据分割符被分割为字符数组打印出来。
另外Split可以重载,能够把字符串中空白字符移除掉:
Split使用方法(2):string[] strs2 = s1.Split(new char {'分割符'},StringSplitOptions.RemoveEmptyEntries);
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 字符串分割{ class Program { static void Main(string[] args) { string s1 = "aaa,bbb.ccc\\ddd,ee,,f"; string[] strs1 = s1.Split(',','.','\\'); string[] strs2 = s1.Split(new char[] { ',', '.', '\\' },StringSplitOptions.RemoveEmptyEntries); foreach (string item in strs1) { Console.WriteLine(item); } foreach (string item in strs2) { Console.WriteLine(item); } Console.ReadKey(); } }}
执行结果(字符串中的空格被移除):
Split使用方法(3):string[] strs3 = s1.Split(new shring{"字符串分割符"},StringSplitOptions.RemoveEmptyEntries);
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 字符串分割{ class Program { static void Main(string[] args) { string s1 = "我是刘德华我是甄子丹我是张学友"; string[] strs1=s1.Split(new string[]{"我是"},StringSplitOptions.RemoveEmptyEntries); foreach (string item in strs1) { Console.WriteLine(item); } Console.ReadKey(); } }}
执行结果(按字符串对字符串进行分割):
二、字符串替换:string Replace(string oldValue,string newValue)
将字符串中出现oldValue的地方替换为newValue
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 字符串替换{ class Program { static void Main(string[] args) { string s = "李雷成绩很好,向李雷同学学习"; s = s.Replace("李雷", "韩梅梅"); Console.WriteLine(s); Console.ReadKey(); } }}
执行结果:
三、取子字符串:
1、string Substring(int startIndex),取从位置startIndex开始一直到最后的子字符串
2、string Substring(int startIndex,int length),取从位置startIndex开始长度为length的子字符串,如果子字符串的长度不足length则报错
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace SubString函数{ class Program { static void Main(string[] args) { string s = "http://www.baidu.com"; string 域名 = s.Substring(7); Console.WriteLine(域名); string s1 = s.Substring(7, 5); Console.WriteLine(s1); Console.ReadKey(); } }}
执行结果:
四、判断字符串是否含有字串
bool Contains(string value)
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 包含{ class Program { static void Main(string[] args) { string s = "我们的社会真和谐!"; if (s.Contains("社会") || s.Contains("和谐")) { Console.WriteLine("含有敏感词汇"); } Console.ReadKey(); } }}
执行结果:
其他字符串函数:
bool StartsWith(string value)判断字符串是否以字串value开始
bool EndsWith(string value)判断字符串是否以字串value结束
int IndexOf(string value)取字串value第一次出现的位置,用法如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 字符串综合练习{ class Program { static void Main(string[] args) { string email = Console.ReadLine(); int i=email.IndexOf("@"); string username = email.Substring(0, i); string 域名 = email.Substring(i + 1); Console.WriteLine("用户名:" + username); Console.WriteLine("域名:" + 域名); Console.ReadKey(); } }}
执行结果:
- C#学习:字符串函数详解
- 【C#学习】字符串函数
- 详解C#格式化字符串
- 黑马程序员之C#学习笔记:字符串函数
- C#字符串函数
- C# 字符串格式化函数
- C#字符串函数
- C#字符串函数
- c# 字符串函数
- C#的字符串函数
- C#字符串函数
- C#字符串函数
- C#字符串函数
- C# 字符串函数
- C#字符串函数
- C#字符串函数 总结
- c#字符串函数
- C#字符串函数
- 大话C++----关于类的那些基础事
- win7不用路由器、猫、一条网线多人用,即使IP绑定了都行!
- Red Hat 9配置FTP
- win7教你如何做到批量重命名
- 微软认证:怎样让Win7在睡眠中继续下载
- C#学习:字符串函数详解
- win7 右键添加 显示\隐藏 系统文件+扩展名
- .com与.exe的区别
- 一致性哈稀解释
- android短信开发
- WIN7 直装版安装教程
- Eclipses中的launch文件
- 包含在其他区间内的区间
- 如何成为一名优秀的C程序员