怎样解析一个字符串“00000,10003,10001”
2008-03-29 19:54
344 查看
采用
indexOF函数
和
substring函数
(一)indexOF方法http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpref/html/frlrfsystemstringclasssubstringtopic.asp
indexof在不同的场合,有不同的作用和用法.
比如检索一个字符在一个单词里在第几个位置.就可以用IndexOf
例:
string str="hello";
int i = str.IndexOf("e");//i=1
这只是一种,在其它的类里面也有这个属性或方法,用法都不同的.
MSDN里面讲的很清楚.
//一般都配合Substring(截取函数)一起使用
string a="123+456";
int index=a.IndexOf('+');//得到字符'+'的位置
string num1=a.Substring(0,index);//从0开始截取出index个字符
string num2=a.Substring(index+1,a.Length-index-1);//从index+1开始截取出a的长度减去(index+1)个字符
(二)substring函数http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpref/html/frlrfsystemstringclasssubstringtopic.asp
在C#中有substring(int startindex,int len)方法。也就是substring()函数。
如:string str=abcdef;
str.substring(1,2)即可得到bc;
怎样取字符串左边第1个字符?
比如 aa="abc"
aa.substring(0,1) 取到的是a
aa.substring(1, aa.Length - 1) 取的是 bc
(三)C#中substring ()的用法
String.SubString(int index,int length)
index:开始位置,从0开始
length:你要取的子字符串的长度
示例:
using System;
using System.Collections.Generic;
using System.Text;
namespace str_sub
{
class Program
{
static void Main(string[] args)
{
string myString = "Hello Word!";
//Substring()在C#中有两个重载函数
//分别如下示例
string subString1 = myString.Substring(0);
//如果传入参数为一个长整, 且大于等于0,
//则以这个长整的位置为起始,
//截取之后余下所有作为字串.
//如若传入值小于0,
//系统会抛出ArgumentOutOfRange异常
//表明参数范围出界
string subString2 = myString.Substring(0, 5);
//如果传入了两个长整参数,
//前一个为参数子串在原串的起始位置
//后一个参数为子串的长度
//如不合条件同样出现上述异常
Console.WriteLine(subString1);
Console.WriteLine(subString2);
Console.ReadLine();
}
}
}
程序输出的结果:
Hello Word!
Hello
(四)String.IndexOf 方法(C#)
String.IndexOf 方法 (Char, [startIndex], [count])
报告指定字符在此实例中的第一个匹配项的索引。搜索从指定字符位置开始,并检查指定数量的字符位置。
要查找的 Unicode 字符。 对 value 的搜索区分大小写。
startIndex(Int32)
可选项,搜索起始位置。不设置则从0开始。
count(Int32)
可选项,要检查的字符位置数。
备注:是按照从左向右的瞬息查找的,如果从右向左,则调用lastIndexOf。
(来源:MSDN:http://msdn2.microsoft.com/zh-cn/library/ms131434(VS.80).aspx)
indexOF函数
和
substring函数
(一)indexOF方法http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpref/html/frlrfsystemstringclasssubstringtopic.asp
indexof在不同的场合,有不同的作用和用法.
比如检索一个字符在一个单词里在第几个位置.就可以用IndexOf
例:
string str="hello";
int i = str.IndexOf("e");//i=1
这只是一种,在其它的类里面也有这个属性或方法,用法都不同的.
MSDN里面讲的很清楚.
//一般都配合Substring(截取函数)一起使用
string a="123+456";
int index=a.IndexOf('+');//得到字符'+'的位置
string num1=a.Substring(0,index);//从0开始截取出index个字符
string num2=a.Substring(index+1,a.Length-index-1);//从index+1开始截取出a的长度减去(index+1)个字符
(二)substring函数http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpref/html/frlrfsystemstringclasssubstringtopic.asp
在C#中有substring(int startindex,int len)方法。也就是substring()函数。
如:string str=abcdef;
str.substring(1,2)即可得到bc;
怎样取字符串左边第1个字符?
比如 aa="abc"
aa.substring(0,1) 取到的是a
aa.substring(1, aa.Length - 1) 取的是 bc
(三)C#中substring ()的用法
String.SubString(int index,int length)
index:开始位置,从0开始
length:你要取的子字符串的长度
示例:
using System;
using System.Collections.Generic;
using System.Text;
namespace str_sub
{
class Program
{
static void Main(string[] args)
{
string myString = "Hello Word!";
//Substring()在C#中有两个重载函数
//分别如下示例
string subString1 = myString.Substring(0);
//如果传入参数为一个长整, 且大于等于0,
//则以这个长整的位置为起始,
//截取之后余下所有作为字串.
//如若传入值小于0,
//系统会抛出ArgumentOutOfRange异常
//表明参数范围出界
string subString2 = myString.Substring(0, 5);
//如果传入了两个长整参数,
//前一个为参数子串在原串的起始位置
//后一个参数为子串的长度
//如不合条件同样出现上述异常
Console.WriteLine(subString1);
Console.WriteLine(subString2);
Console.ReadLine();
}
}
}
程序输出的结果:
Hello Word!
Hello
(四)String.IndexOf 方法(C#)
String.IndexOf 方法 (Char, [startIndex], [count])
报告指定字符在此实例中的第一个匹配项的索引。搜索从指定字符位置开始,并检查指定数量的字符位置。
参数
value要查找的 Unicode 字符。 对 value 的搜索区分大小写。
startIndex(Int32)
可选项,搜索起始位置。不设置则从0开始。
count(Int32)
可选项,要检查的字符位置数。
返回值
如果找到该字符,则为 value 的索引位置;否则如果未找到,则为 -1。备注:是按照从左向右的瞬息查找的,如果从右向左,则调用lastIndexOf。
(来源:MSDN:http://msdn2.microsoft.com/zh-cn/library/ms131434(VS.80).aspx)
相关文章推荐
- 怎样解析一个字符串“00000,10003,10001”(同上)
- jquery中怎样把字符串解析成json【前端$.parseJSON(字符串)返回json对象详细看文章内容】
- 怎样判断一个字符,在另一个字符串中
- Java中怎样判断一个字符串是否是数字
- Java爬虫(二)--解析一个HTML字符串
- 串口中怎样接收一个完整数据包的解析
- java 将一个字符串解析为日期 将日期转为自己定义的格式以字符串输出
- php怎样判断一个字符串是否包含另一个字符串?
- simpleXML怎样解析一个xml文件,并对xml进行增删改查 过程
- JS中Eval解析JSON字符串的一个小问题
- 怎样使用Gson 解析 (deserialize) json字符串
- 串口中怎样接收一个完整数据包的解析
- 【公司动态添加行】前台穿一个json的字符串到后台,并解析
- IOS中怎样把一个字符串倒序输出
- js中使用parseFloat()将一个字符串解析成数字
- jquery中解析一个 JSON 字符串
- 在java中,怎样把一个double数转换为字符串时,不用科学计数法表示。
- php一个解析字符串排列数组的方法
- 怎样取php一个字符串中的某个字符
- 【使用JSOUP实现网络爬虫】解析一个HTML字符串