坚持139

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

使用String.Index 和 String.Split,对字符串中的内容进行分割查询。
要查询一个string的内容,请参考下面:

 

            string source = "Global PD/eP40/CFD-CN/NC";
char[] separator = { '/' };
string[] result;


int firstSlashLoc = source.IndexOf('/'); // 9
string firstPart = source.Substring(0, firstSlashLoc); // Global PD
int secondSlashLoc = source.IndexOf('/', firstSlashLoc + 1); // 14
string secondPart = source.Substring(firstSlashLoc + 1, secondSlashLoc - firstSlashLoc - 1); // eP40
string thirdPart = source.Substring(secondSlashLoc + 1); // CFD-CN/NC


// Display the original string and delimiter string.
Console.WriteLine("Splitting the string:\n \"{0}\".", source);
Console.WriteLine();

// Split a string delimited by another string and return all elements.
result = source.Split(separator);
Console.WriteLine("Result including all elements ({0} elements):",
result.Length);
Console.Write("");
foreach (string s in result)
{
Console.Write("'{0}' ", String.IsNullOrEmpty(s) ? "<>" : s);
}
Console.WriteLine();
Console.ReadLine();

显示结果如下:

 

其中,Substring(Int32),表示取出字符串从指定位置开始到结束。Substring(Int32, Int32),表示从指定位置开始且具有指定的长度。

 

posted on 2011-12-01 15:40  坚持139  阅读(374)  评论(0编辑  收藏  举报