C#字符串类的典型用法
2012-09-12 18:24
302 查看
using System; namespace e1_4_11 { class Program { static void Main(string[] args) { //字符串搜索 string s = "ABC科学"; int j = s.IndexOf("科"); Console.WriteLine("{0}", j); string s1 = "abc"; string s2 = "abc"; string s3 = "相同"; //判断连个字符串是否相同 if (s1 == s2) Console.WriteLine("{0}", s3); else { s3 = "不相同"; Console.WriteLine("{0}", s3); } //判断字符串是否为空 string s4 = ""; string s5 = "不空"; if (s4.Length == 0) { s5 = "空"; Console.WriteLine("{0}", s5); } string s6 = "取子字符串"; //从索引为2开始取2个字符,sb="字符",s内容不变 string sb = s6.Substring(2, 2); //sb="字符" char sb1 = s6[0]; Console.WriteLine(sb); Console.WriteLine(sb1); //删除字符串 string sb2 = s6.Remove(0, 2); //从索引为0开始删除2个字符,sb="字符串",s6内容不变 Console.WriteLine(sb2); //插入字符串 string s7 = "计算机科学"; string s8 = s7.Insert(3, "软件"); Console.WriteLine(s8); //替换字符串 string s9 = s7.Replace("计算机", "软件"); Console.WriteLine(s9); //将字符串转换为字符数组 char[] s10 = s7.ToCharArray(0, s7.Length); Console.WriteLine(s10); //转换为字符串 int i = 9; string s11 = i.ToString(); Console.WriteLine(s11); //大小写转换 string st = "AaBbCc"; string s12 = st.ToLower(); string s13 = st.ToUpper(); Console.WriteLine(s12); Console.WriteLine(s13); //删除所有空格 string sk = "A bc"; Console.WriteLine(sk.Trim()); } } }
相关文章推荐
- C#字符串类的典型用法
- C#随机函数random()典型用法集锦
- C#随机函数random()典型用法集锦
- C#随机函数random()典型用法集锦
- unity 3D c# 协程的典型用法-Unity官方案例精讲笔记
- C#随机函数random()典型用法集锦
- 实例分享C#中Explicit和Implicit用法
- C# dataGridView用法
- 详解C#中的Async和Await用法
- C# MessageBox.Show 用法(转)
- 在C#代码中应用Log4Net(二)典型的使用方式
- C#中的using和namespace用法
- C#中一些字符串操作的常用用法
- C#中SqlParameter的作用与用法
- c# using 关键字的用法
- C#Hashtable的用法
- C#中构造函数重载的用法
- C#基础---IComparable用法,实现List<T>.sort()排序
- C#:RadioButton控件的用法
- C#中线程的用法