IndexOf、LastIndexOf、Substring的用法
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 - ];
}
else
{
string[] arr = path.Split('/');
return arr[arr.Length - ];
}
} /// <summary>
/// 获取文件后缀名
/// </summary>
/// <param name="filename">文件名</param>
/// <returns></returns>
public static String GetFex(string filename)
{
return filename.Substring(filename.LastIndexOf(".") + );
} /// <summary>
/// 获取文件后缀名
/// </summary>
/// <param name="filename">文件名</param>
/// <returns></returns>
public static String GetFex(string filename)
{
return filename.Substring(filename.LastIndexOf(".") + );
} /// <summary>
/// 获取文件目录
/// </summary>
/// <param name="filename">文件名</param>
/// <returns></returns>
public static String GetDirectory(string filename)
{
return filename.Substring(, filename.LastIndexOf("/"));
}
C# foreach 中获取索引index的方法
foreach(var item in arr)
{
int index = arr.indexOf(item); //index 为索引值
item....
}
IndexOf、LastIndexOf、Substring的用法的更多相关文章
- js中substr,substring,indexOf,lastIndexOf等的用法
1.substrsubstr(start,length)表示从start位置开始,截取length长度的字符串. var src="images/off_1.png";alert( ...
- JAVA中字符串函数subString的用法小结
本篇文章主要是对JAVA中字符串函数subString的用法进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助 String str; str=str.substring(int begi ...
- IndexOf() LastIndexOf() Contains() StartsWith() EndsWith()方法比较
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- String中的Indexof,LastIndexOf, Indexofany,LastIndexOfAny 的区别
本文转载自 http://www.cnblogs.com/qinying/archive/2008/09/22/1295730.html 定位子串是指在一个字符串中寻找其中包含的子串或者某个字符.在S ...
- js中substring/substr和C#中Substring的用法
一:在js中截取字符串的方法有两个:substring和substr 1.方法: substring(int stringIndex,[int endIndex]) 截取从索引为stringIndex ...
- 使用js的indexOf,lastIndexOf,slice三函数轻易得到url的服务器,路径和页名
js的indexOf,lastIndexOf,slice能帮我们在js字符串处理时少走一些弯路. 程序如下: var url="http://www.cnblogs.com/xiandeda ...
- C#中substring ()的用法
C#中substring ()的用法:http://www.cnblogs.com/bluespace/archive/2007/12/11/782336.html
- IndexOf、LastIndexOf、Substring的用法及C# foreach 中获取索引index的方法
String.IndexOf String.IndexOf 方法 (Char, Int32, Int32)报告指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置 ...
- java中 IndexOf()、lastIndexOf()、substring()的用法
public int indexof(String str)返回字符串中出现str的第一个位置 public int indexof(String str,int fromIndex)返回字符串中从f ...
随机推荐
- webstorm 注册码
User Name: EMBRACE License Key: ===== LICENSE BEGIN ===== 24718-12042010 00001h6wzKLpfo3gmjJ8xoTPw5m ...
- 在Ubuntu Server下搭建LAMP环境学习记录
更新于2015/6/16日,因图片地址失效,请在此地址查看:http://note.youdao.com/share/?id=1c249ae6dc6150cbf692adec67b23a33& ...
- JavaScript中判断为整数的多种方式
之前记录过JavaScript中判断为数字类型的多种方式,这篇看看如何判断为整数类型(Integer). JavaScript中不区分整数和浮点数,所有数字内部都采用64位浮点格式表示,和Java的d ...
- python collections,函数等笔记
笔记 # -*- coding:utf-8 -*- #需求:一个列表里大于66的元素添加字典的第二个key k2里,小于66的添加第一个 key k1里 li = [1,2,3,4,5,6,67,7, ...
- folly
一.简介 Folly是,Facebook于2012年6月初开源的一个基于C++11的C++组件库,提供了类似Boost库和std库的功能,包括散列.字符串.向量.内存分配.位处理等,以满足大规模高性能 ...
- [转]IOS上路_01-Win7+VMWare9+MacOSX10.8+XCode4.6.3
本文转自:http://my.oschina.net/vigiles/blog/141689 目录[-] 1. 资源准备: 1)实体机: 2)VMWare9: 3)VM for MacOS 补丁: 4 ...
- java基础疑难点总结之成员变量的继承,方法重载与重写的区别,多态与动态绑定
1.成员变量的继承 1.1要点 子类用extends关键字继承父类.子类中可以提供新的方法覆盖父类中的方法.子类中的方法不能直接访问父类中的私有域,子类可以用super关键字调用父类中的方法.在子类中 ...
- java读取word内容
暂时只写读取word内容的方法. 依赖的jar: poi-3.9-20121203.jarpoi-ooxml-3.9-20121203.jarxmlbeans-2.3.0.jar package co ...
- codeforces gym-101078
题目链接: http://codeforces.com/gym/101078 A: #include <iostream> #include <cstdio> #include ...
- ACdream OJ 1099 瑶瑶的第K大 --分治+IO优化
这题其实就是一个求数组中第K大数的问题,用快速排序的思想可以解决.结果一路超时..原来要加输入输出优化,具体优化见代码. 顺便把求数组中第K大数和求数组中第K小数的求法给出来. 代码: /* * th ...