怎样解析一个字符串“00000,10003,10001”

来源:互联网 发布:淘宝网买机票 编辑:程序博客网 时间:2024/05/18 00:15

采用
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