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,这个也是我一直用的

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

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





/// <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 ...
随机推荐
- 10分钟制作UWP汉堡菜单
什么是汉堡菜单? 汉堡菜单,指的是一个可以弹出和收回的侧边栏.在UWP和Android应用中,汉堡菜单都非常常见. 首先我们列出所有需要掌握的前置知识: 1,SplitView 2,StackPane ...
- HDU 3374 String Problem (KMP+最大最小表示)
KMP,在有循环节的前提下: 循环节 t = len-next[len], 个数num = len/(len-next[len]);个人理解,如果有循环节,循环节长度必定小于等于len/2, 换句话说 ...
- input输入框focus获得焦点边缘发亮
从某个插件上摘下来的代码 <html> <head> <title> New Document </title> <style> texta ...
- Linux学习之七——乱码的解决方案
一.乱码的原因 乱码是编码不统一引起的,有下面一些地方需要注意 1. Linux 系统默认支持的语系数据:这与 /etc/sysconfig/i18n 有关:2. 你的终端界面 (bash) 的语系: ...
- java 字节流和字符流的区别 转载
转载自:http://blog.csdn.net/cynhafa/article/details/6882061 java 字节流和字符流的区别 字节流与和字符流的使用非常相似,两者除了操作代码上的不 ...
- 本地Git仓库与Github远程仓库同步
在本地创建了一个Git仓库后,还想在Github创建一个Git仓库,并使其远程同步.1.在电脑的用户主目录下有无.ssh目录,若有看是否有id_rsa和id_rsa.pub文件.若无,则创建SSH K ...
- hdu-5920 Ugly Problem(贪心+高精度)
题目链接: Ugly Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- jquery/js实现验证聚焦,失焦
jquery实现验证聚焦,失焦方法: 我还是喜欢用jquery来实现,不管页面中多少个输入框需要实现聚焦,失焦,都公有,我常用的方法是: 遍历该页面中的input框,获取输入框中的val值,当该输入框 ...
- RabbitMQ 一二事 - 简单队列使用
消息队列目前流行的有三种 1. RabbitMQ 2. ActiveMQ 3. Kafka 这三种都非常强大,RabbitMQ目前用的比较多,也比较流行,阿里也在用 ActiveMQ是阿帕奇出品,但是 ...
- 每日一语:What is he getting at?
What is he getting at? 他讲这话是什么意思? 2015-1-12