• IndexOf、LastIndexOf、Substring的用法及C# foreach 中获取索引index的方法


     String.IndexOf

      String.IndexOf 方法 (Char, Int32, Int32)
    报告指定字符在此实例中的第一个匹配项的索引。搜索从指定字符位置开始,并检查指定数量的字符位置。
    String.IndexOf(value, startIndex, count)

    参数
    value:要查找的 Unicode 字符。 
    startIndex:搜索起始位置。 
    count:要检查的字符位置数。
    返回值(Int32):
    如果找到该字符,则为 value 的索引位置;否则如果未找到,则为 -1。

    String.LastIndexOf

    String.LastIndexOf 方法
    报告指定的 Unicode 字符或 String 在此实例中的最后一个匹配项的索引位置。

    名称 说明  
    String.LastIndexOf (Char) 报告指定 Unicode 字符在此实例中的最后一个匹配项的索引位置。
    String.LastIndexOf (String) 报告指定的 String 在此实例内的最后一个匹配项的索引位置。
    String.LastIndexOf (Char, Int32) 报告指定 Unicode 字符在此实例中的最后一个匹配项的索引位置。该搜索从指定字符位置开始。
    String.LastIndexOf (String, Int32) 报告指定的 String 在此实例内的最后一个匹配项的索引位置。该搜索从指定字符位置开始。
    String.LastIndexOf (String, StringComparison) 报告指定字符串在当前 String 对象中最后一个匹配项的索引。一个参数指定要用于指定字符串的搜索类型。
    String.LastIndexOf (Char, Int32, Int32) 报告指定的 Unicode 字符在此实例内的子字符串中的最后一个匹配项的索引位置。搜索从指定字符位置开始,并检查指定数量的字符位置。
    String.LastIndexOf (String, Int32, Int32) 报告指定的 String 在此实例内的最后一个匹配项的索引位置。搜索从指定字符位置开始,并检查指定数量的字符位置。
    String.LastIndexOf (String, Int32, StringComparison) 报告指定字符串在当前 String 对象中最后一个匹配项的索引。参数指定当前字符串中的起始搜索位置,以及要用于指定字符串的搜索类型。
    String.LastIndexOf (String, Int32, Int32, StringComparison) 报告指定的 String 对象在此实例内的最后一个匹配项的索引位置。参数指定当前字符串中的起始搜索位置、要搜索的当前字符串中的字符数量,以及要用于指定字符串的搜索类型。



    示例:
    string str = "深圳市盈基实业有限公司国际通邓事文*深圳市盈基实业有限公司国际通邓事文";
    Label1.Text = str.LastIndexOf("邓文").ToString();//返回-1
    Label1.Text = str.LastIndexOf("邓").ToString();//返回32

    Label1.Text = str.LastIndexOf("邓",8).ToString();//返回-1
    Label1.Text = str.LastIndexOf("邓",20).ToString();//返回14
    Label1.Text = str.LastIndexOf("邓",33).ToString();//返回32
    说明:在指定的范围内查找字符,这个范围是上面的输入的参数,理解为,从索引0开始到指定的数值位置范围内查找最后一个匹配的的字符串的位置。示例中,0-8中没有“邓”字,所以返回-1,0-20范围中,有一个“邓”字在索引14位置上,0-33范围中有两个“邓”字,因为LastIndexOf是返回最后一个匹配项索引位置,所以返32,而不是14。

    String.Substring

    String.Substring 方法
    从此实例检索子字符串。 

    名称 说明
    String.Substring (Int32) 从此实例检索子字符串。子字符串从指定的字符位置开始。
    String.Substring (Int32, Int32) 从此实例检索子字符串。子字符串从指定的字符位置开始且具有指定的长度。



    示例:
    string str = "深圳市盈基实业有限公司国际通邓事文*深圳市盈基实业有限公司国际通邓事文";
    Label1.Text = str.Substring(11);//返回 “国际通邓事文*深圳市盈基实业有限公司国际通邓事文”
    Label1.Text = str.Substring(11,7);//返回 “国际通邓事文*”

    Label1.Text = str.Substring(str.Length-3,3); // 返回邓事文,即截倒数3位字符

    总结:

    IndexOf、LastIndexOf都是返回一个位置,是个整数值;找不到都返回-1;
    IndexOf是从左向右查,LastIndexOf是从右向左查,不管是IndexOf还是LastIndexOf,索引序列都是从左到右的(起始值是0)
    Substring是字符串截取,返回值是一个截取后的字符串。

    个人使用示例:

    @{
    string str = content.CustomField02;
    string file = str.Substring(str.LastIndexOf("/") + 1); //去掉了路径
    string finame = file.Substring(0, file.LastIndexOf(".")); //去掉了后缀名
    }

    sql 取字符串后几位问题
    SQL行内容的字符数不定,现要让其显示左边第二个字符后的字符,方法:right(字符串,len(字符串)-2)
    例如,原先字段DefaultPicUrl的某条数结果是:UploadFiles/2007113010616149.jpg,现要变成:/uploadfiles/newbook/150729/2007113010616149.jpg
    写法如下:
    select '/uploadfiles/newbook/150729/'+right(DefaultPicUrl,LEN(DefaultPicUrl)-12) as picurl from dbo.Product

    http://www.cnblogs.com/chiname/articles/151359.html

    删除字符串最后一个字符的几种方法

    字符串:string s = "1,2,3,4,5," 

    目标:删除最后一个 "," 

    方法: 
       1、用的最多的是Substring,这个也是我一直用的  

    s = s.Substring(0,s.Length - 1)



       2、用 RTrim,这个我原来只知道用来删除最后的空格,也没有仔细看过其它的用法,才发现可以直接trim掉一些字符 
                

    s = s.ToString().RTrim(',')


       3、用TrimEnd,这个东西和RTrim差不多,区别是这个传递的是一个字符数组,而RTrim可以是任何有效的字符串 

    s=s.TrimEnd(',') 
    //如果要删除"5,",则需要这么写 
    char[] MyChar = {'5',','}; 
    s = s.TrimEnd(MyChar); 
    //s = "1,2,3,4" 
     /// <summary>
     /// 获取文件名称 
     /// </summary>
     /// <param name="path">路径</param>
     /// <returns></returns>
     public static string GetFileName(String path)
     {
     if (path.Contains("\"))
     {
     string[] arr = path.Split('\');
     return arr[arr.Length - 1];
     }
     else
     {
     string[] arr = path.Split('/');
     return arr[arr.Length - 1];
     }
     }
    
    
     /// <summary>
     /// 获取文件后缀名 
     /// </summary>
     /// <param name="filename">文件名</param>
     /// <returns></returns>
     public static String GetFex(string filename)
     {
     return filename.Substring(filename.LastIndexOf(".") + 1);
     }
    
     /// <summary>
     /// 获取文件后缀名 
     /// </summary>
     /// <param name="filename">文件名</param>
     /// <returns></returns>
     public static String GetFex(string filename)
     {
     return filename.Substring(filename.LastIndexOf(".") + 1);
     }
    
     /// <summary>
     /// 获取文件目录 
     /// </summary>
     /// <param name="filename">文件名</param>
     /// <returns></returns>
     public static String GetDirectory(string filename)
     {
     return filename.Substring(0, filename.LastIndexOf("/"));
     }

     C# foreach 中获取索引index的方法

    foreach(var item in arr)
            {
                    int index = arr.indexOf(item); //index 为索引值
                    item....
            }
  • 相关阅读:
    575. Distribute Candies
    242. Valid Anagram
    349. Intersection of Two Arrays
    290. Word Pattern
    389. Find the Difference
    httpModules 与 httpHandlers
    JS获取浏览器信息及屏幕分辨率
    asp.net 页面编码 设置的几种方法
    IIS是如何处理ASP.NET请求的
    VS2010常用插件介绍之Javascript插件(一)
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/4646058.html
Copyright © 2020-2023  润新知