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 ...
随机推荐
- 以太网卡TSO技术
一.简介 TSO(TCP Segment Offload)技术是一种利用网卡的少量处理能力,降低CPU发送数据包负载的技术,需要网卡硬件及驱动的支持. 二.原理 在不支持TSO的网卡上,TCP层向 ...
- Hadoop could not find or load main class
Error: Could not find or load main class <class_name> 我在尝试使用hadoop definitive guide的代码做练习时,遇到一 ...
- 频谱分析仪 RBW&VBW
扫频式频谱分析仪的结构如下图 RBW(Resolution Bandwidth)的影响 The RBW dictates the resolution bandwidth, which is rela ...
- uva 11134 fabled rooks (贪心)——yhx
We would like to place n rooks, 1 n 5000, on a n nboard subject to the following restrictions• The i ...
- uva 10976 fractions again(水题)——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAB3gAAAM+CAIAAAB31EfqAAAgAElEQVR4nOzdO7KtPJum69GEpAcVQQ ...
- 漫谈计算摄像学 (一):直观理解光场(Light Field)
什么是计算摄像学 计算摄像学(Computational Photography)是近年来越来越受到注意的一个新的领域,在学术界早已火热.本来计算摄像学的业界应用在群众中一直没什么知名度,直到Lytr ...
- UVALive 6450 Social Advertising DFS解法
题意:一些人有朋友关系,在某个人的社交网站上投放广告可以被所有该人的直接朋友看到,问最小投放多少个广告使给出的人都看到广告.(n<=20) 解法:看到n的范围可以想到用二进制数表示每个人被覆盖与 ...
- HDU 1251 统计难题
字典树又一基本题 代码: #include <iostream> #include <cstdio> #include <cstring> #include < ...
- ZOJ 3659 & HDU 4424 Conquer a New Region (并查集)
这题要用到一点贪心的思想,因为一个点到另一个点的运载能力决定于其间的边的最小权值,所以先把线段按权值从大到小排个序,每次加的边都比以前小,然后合并集合时,比较 x = findset(a) 做根或 y ...
- Google play billing(Google play 内支付) 上篇
写在前面: 最近Google貌似又被全面封杀了,幸好在此之前,把Google play billing弄完了,现在写篇 博客来做下记录.这篇博客一是自己做个记录,二是帮助其他有需要的人.因为现在基本登 ...