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 ...
随机推荐
- 设计模式C#实现(五)——抽象工厂模式
抽象工厂模式 意图:提供一个创建一系列相关或相互依赖对象的接口,而无需指定他们具体的类. UML类图: 场景:抽象村商店想销售Pizza和Hamburg等多种商品,而在美国店和中国店要出售本土化的口味 ...
- centos 配置本地 yum源
修改CentOS-Media.repo时 每行代码开始不要有空格 [c6-media] #库名称 name=CentOS-$releasever - Media #名称描述 baseurl=file: ...
- python datetime模块用strftime 格式化时间
1 2 3 #!usr/bin/python import datetime datetime.datetime.now() 这个会返回 microsecond.因此这个是我们不需要的.所以得做一下修 ...
- UWP 矢量字体图标(iconfont)使用
本文使用 阿里巴巴开源字体: 选择矢量字体图标: 查看或编辑 Unicode编码 或字体名称 下载到本地,添加到uwp项目 代码中写法 Text:Unicode编码 FontFamily:文件路径#字 ...
- 《TCP/IP 详解 卷一》读书笔记-----Ping&Traceroute
1.ping是用于测试对方主机是否可达的命令,其实本质上就是echo类型的ICMP报文.同时,ping还能用于计算RTT(round-trip time),即两台主机间的往返时延. 2.随着网络安全意 ...
- 矩阵乘法快速幂 codevs 1732 Fibonacci数列 2
1732 Fibonacci数列 2 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 在“ ...
- Treap(树堆):随机平衡二叉树实现
本文是根据郭家宝的文章<Treap的原理及实现>写的. #include<stdio.h> #include<string.h> #include<stdli ...
- 测试杂感:Bug Bash
缺陷大扫除(Bug Bash)是一项短期的全员测试活动.在微软,许多开发团队会在里程碑(milestone)的末期执行缺陷大扫除.程序员.测试员.程序经理.内部用户.市场人员在1~3天的时间窗口中,运 ...
- ant 自动构建血泪史
1. android.bat update project -p . -t xxx 其中: xxx 为 targetid 特别注意的是: targetid 不等于 API Level.... 2. ...
- unix文件操作函数
1. fopen函数 #include <stdio.h> FILE *fopen(const char *path, const char *mode) 返回:文件顺利打开后,指向该流的 ...