黑马程序员——C#中字符串方法
来源:互联网 发布:gta5女性捏脸数据库 编辑:程序博客网 时间:2024/05/18 03:37
---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------
2) 注意字符串是不可变的,所以这些函数都不会直接改变字
6) string[] Split(params char[] separator):
将字符串按照指定的分割符分割为字符串数组;
8) 字符串替换:string Replace(string oldValue, string newValue)
将字符串中的出现oldValue的地方替换为newValue
string date = "2011/7/15";
date = date.Replace("/", "-"); //将输入的符号替换成-符号
10) bool Contains(string value)判断字符串中是否含有子串value;
string[] words = { "自由","和谐","xxoo" };
Console.WriteLine("请输入你想说的一句话");
string cen = Console.ReadLine();
int i;
for (i = 0; i < words.Length; i++)
{
if (cen.Contains(words[i]))
{
break;
}
}
if (i < words.Length)
{
Console.WriteLine("你输入的内容有非法字符");
}
else
{
Console.WriteLine("你输入的 {0} ,已发表成功!",cen);
}
11) int IndexOf(string value):取子串value第一次出现的位置。
如果搜索不到指定的字串则返回-1
string str = "Hello,World!";
Console.WriteLine(str.IndexOf("o"));
//搜索o的第一个位置
Int IndexOf(string value,int startIndex).
1) ToLower():得到字符串的小写形式。
2) 注意字符串是不可变的,所以这些函数都不会直接改变字
符串的内容,而是把修改后的字符串的值通过函数返回值
的形式返回。s.ToLower()与s=s.ToLower()
的形式返回。s.ToLower()与s=s.ToLower()
3) ToUpper():得到字符串的大写形式; Trim()去掉字符串两端的空白。
4) s1.Equals(s2, StringComparison.OrdinalIgnoreCase),两
个字符串进行比较不区分大小写的比较
个字符串进行比较不区分大小写的比较
5) s1==s2
Trim(); 去掉两边的空白.
s1.Equals :字符串大小的比较
6) string[] Split(params char[] separator):
将字符串按照指定的分割符分割为字符串数组;
string str = "my name is lilei";
string[] words = str.Split(' ');
string[] words = str.Split(' ');
Console.WriteLine("上句话中共有{0}个单词",words.Length);
Console.WriteLine("这几句单词分别为");
for (int i = 0; i < words.Length; i++)
{
Console.WriteLine(words[i]);
}
Console.ReadKey();
Console.WriteLine("这几句单词分别为");
for (int i = 0; i < words.Length; i++)
{
Console.WriteLine(words[i]);
}
Console.ReadKey();
7) str,StringSplitOptions.RemoveEmptyEntries(除结果中的空白字符串);
string str = "my name is lilei!";
char[] strs = { ' ', '!' };
string[] words = str.Split(strs,StringSplitOptions.RemoveEmptyEntries);
string str = "my name is lilei!";
char[] strs = { ' ', '!' };
string[] words = str.Split(strs,StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine("上句话中共有{0}个单词",words.Length);
Console.WriteLine("这几句单词分别为");
for (int i = 0; i < words.Length; i++)
{
Console.WriteLine(words[i]);
}
Console.ReadKey();
Console.WriteLine("这几句单词分别为");
for (int i = 0; i < words.Length; i++)
{
Console.WriteLine(words[i]);
}
Console.ReadKey();
8) 字符串替换:string Replace(string oldValue, string newValue)
将字符串中的出现oldValue的地方替换为newValue
string date = "2011/7/15";
date = date.Replace("/", "-"); //将输入的符号替换成-符号
string[] sp = date.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine("你刚刚输入的时间为::{0}年{1}月{2}日",sp);
Console.WriteLine("你刚刚输入的时间为::{0}年{1}月{2}日",sp);
9) 取子字符串:string Substring(int startIndex),
取从位置startIndex开始一直到最后的子字符串;
取从位置startIndex开始一直到最后的子字符串;
string Substring(4,2)
取从位置4开始一直到4开始的第二个字符串
取从位置4开始一直到4开始的第二个字符串
10) bool Contains(string value)判断字符串中是否含有子串value;
string[] words = { "自由","和谐","xxoo" };
Console.WriteLine("请输入你想说的一句话");
string cen = Console.ReadLine();
int i;
for (i = 0; i < words.Length; i++)
{
if (cen.Contains(words[i]))
{
break;
}
}
if (i < words.Length)
{
Console.WriteLine("你输入的内容有非法字符");
}
else
{
Console.WriteLine("你输入的 {0} ,已发表成功!",cen);
}
bool StartsWith(string value)判断字符串是否以子串value开始;
bool EndsWith (string value)判断字符串是否以子串value结束;
bool EndsWith (string value)判断字符串是否以子串value结束;
11) int IndexOf(string value):取子串value第一次出现的位置。
如果搜索不到指定的字串则返回-1
string str = "Hello,World!";
Console.WriteLine(str.IndexOf("o"));
//搜索o的第一个位置
Int IndexOf(string value,int startIndex).
string str = "Hello,World!";
Console.WriteLine(str.IndexOf("o",5));
Console.WriteLine(str.IndexOf("o",5));
//搜索o的第二个位置
---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net/heima/
- 黑马程序员——C#中字符串方法
- 黑马程序员——c#中string字符串
- 黑马程序员—c#中各种方法总结
- 黑马程序员—C#方法的重载
- 黑马程序员—C#中递归
- 黑马程序员—字符串
- 黑马程序员—字符串
- 黑马程序员--C#中 的虚方法
- 黑马程序员——C#方法与方法重载 .
- 黑马程序员——获取字符串中相同字符个数的方法
- "黑马程序员” c#中分割字符串的几种方法
- 黑马程序员——Foundation框架—字符串方法
- 黑马程序员——手动实现字符串的trim()方法
- 黑马程序员——.NET笔记8 字符串方法总结
- 黑马程序员--C#中字符串的一些知识小结
- 黑马程序员——c#中的构造方法
- 黑马程序员——字符串
- 黑马程序员——字符串
- 字符串最小表示模板
- arm仿真器Skyeye的安装及使用
- 打印所有括号匹配排列方式
- Codeforces140Div.2
- 前缀、中缀和后缀表达式
- 黑马程序员——C#中字符串方法
- 【thinkphp3.x】thinkphp3.x中的Widget功能
- T430 VirtualBox下安装MAC雪豹10.6
- 黑马程序员——SQL基础
- Ubuntu下彻底卸载mysql
- HDU 1561 The more, The Better (treeDP 背包) #by Plato
- poj 3301 Texas Trip (三分求极值)
- Building Coder(Revit 二次开发) - 创建与墙体平行的剖视图
- 黑马程序员——C#面向对象