LastIndexOf干什么用的

LastIndexOf的作用是对字符串进行从后往前的检索,找到第一个匹配的位置。
比如对字符串“abcdbcd”执行lastindexof("bc")操作,得到的结果是4;4是从前往后数第4个位置
示例:
tring.IndexOf
String.IndexOf 方法 (Char, Int32, Int32)
报告指定字符在此实例中的第一个匹配项的索引。搜索从指定字符位置开始,并检查指定数量的字符位置。
String.IndexOf(value, startIndex, count)
参数
value:要查找的 Unicode 字符。 
startIndex:搜索起始位置。 
count:要检查的字符位置数。
返回值(Int32):
如果找到该字符,则为 value 的索引位置;否则如果未找到,则为 -1。 示例: string str = "深圳市盈基实业有限公司国际通邓事文*深圳市盈基实业有限公司国际通邓事文"; Label1.Text = str.IndexOf("中国").ToString();//返回 -1  Label1.Text = str.IndexOf("盈基").ToString();//返回 3  Label1.Text = str.IndexOf("盈基",10).ToString();//返回21 说明:这是从第10个字符开始查起。  Label1.Text = str.IndexOf("邓",15,10).ToString();//返回 -1;从第15个位置开始,后10个范围内 Label1.Text = str.IndexOf("邓",15,20).ToString();//返回 -32 说明:从第15个字符开始查找,要查找的范围是从第15个字符开始后20个字符,即从第15-35个字符中查找。
String.LastIndexOf 方法 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);//返回 “国际通邓事文*”
总结一下:
IndexOf、LastIndexOf都是返回一个位置,是个整数值;找不到都返回-1;
IndexOf是从左向右查,LastIndexOf是从右向左查,不管是IndexOf还是LastIndexOf,索引序列都是从左到右的(起始值是0)
Substring是字符串截取,返回值是一个截取后的字符串。

转载于:https://www.cnblogs.com/ZkbFighting/p/8043208.html

public int convert2Ofd(int imagefileid, String filename,String docdcsurl) { writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------start"); //String docdcsurl = Util.null2String(getPropValue("yzDcsUrl", "docdcsurl")); writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------imagefileid=" + imagefileid + ";filename=" + filename + ";docdcsurl=" + docdcsurl); int newimagefileid = -1; try { if (imagefileid > 0 && !"".equals(filename) && !"".equals(docdcsurl)) { String fileext = ""; if (filename.indexOf(".") != -1) { fileext = filename.substring(filename.lastIndexOf(".")); } writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------fileext=" + fileext); String sourcefilepath = getImageFile(imagefileid + ""); writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------sourcefilepath=" + sourcefilepath); if (!"".equals(sourcefilepath)) { String dcsurl = convert(docdcsurl, sourcefilepath, "29"); writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------dcsurl=" + dcsurl); if (!"".equals(dcsurl)) { InputStream input = getInputStreamFromDcs(dcsurl); writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------input=" + input); String newfilename = filename.substring(0, filename.lastIndexOf(".")) + ".ofd"; if (input != null) { newimagefileid = savePdfImageFile(input, newfilename); } if (new File(sourcefilepath).exists() && new File(sourcefilepath).isFile()) { new File(sourcefilepath).delete(); } } } } } catch (Exception e) { writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------Exception=" + e); } writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------newimagefileid=" + newimagefileid); writeLog("ConvertToPdfForDcsE9--convertUot2Ofd-------------------end"); return newimagefileid; } 其中 String dcsurl = convert(docdcsurl, sourcefilepath, "29");这个是干什么
06-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值