zoukankan      html  css  js  c++  java
  • String.Index 和 String.Split的用法例子

    使用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),表示从指定位置开始且具有指定的长度。

  • 相关阅读:
    重定向与转发比较
    servlet_5
    servlet_4
    servlet_3
    字符串的操作以及格式化的操作
    2019的Python
    函数2
    函数
    文件操作
    集合 set
  • 原文地址:https://www.cnblogs.com/zhoukaiwei/p/2270689.html
Copyright © 2011-2022 走看看