搞一个公司网站得多少钱,wordpress 钩子大全,杭州微信网站制作,建设部特种作业证网站查询在给定的字符串中查找字符或字符串是比较常见的操作。字符串查找分为两种形式#xff1a;一种是在字符串中获取匹配字符#xff08;串#xff09;的索引值#xff0c;另一种是在字符串中获取指定索引位置的字符。
根据字符查找
String 类的 indexOf() 方法和 lastlndexOf…在给定的字符串中查找字符或字符串是比较常见的操作。字符串查找分为两种形式一种是在字符串中获取匹配字符串的索引值另一种是在字符串中获取指定索引位置的字符。
根据字符查找
String 类的 indexOf() 方法和 lastlndexOf() 方法用于在字符串中获取匹配字符串的索引值。
1. indexOf() 方法
indexOf() 方法用于返回字符串在指定字符串中首次出现的索引位置如果能找到则返回索引值否则返回 -1。该方法主要有两种重载形式
str.indexOf(value)str.indexOf(value,int fromIndex)
其中str 表示指定字符串
value 表示待查找的字符串
fromIndex 表示查找时的起始索引如果不指定 fromIndex则默认从指定字符串中的开始位置即 fromIndex 默认为 0开始查找。
例如下列代码在字符串“Hello Java”中查找字母 v 的索引位置。
String s Hello Java;
int size s.indexOf(v); // size的结果为8上述代码执行后 size 的结果为 8它的查找过程如图 1 所示。 例 1 编写一个简单的 Java 程序演示 indexOf() 方法查找字符串的用法并输出结果。代码如下
public static void main(String[] args) {String words today,monday,sunday;System.out.println(原始字符串是words);System.out.println(indexOf(\day\)结果words.indexOf(day));System.out.println(indexOf(\day\,5)结果words.indexOf(day,5));System.out.println(indexOf(\o\)结果words.indexOf(o));System.out.println(indexOf(\o\,6)结果words.indexOf(o,6));
}运行后的输出结果如下
原始字符串是today,monday,sunday
indexOf(day)结果2
indexOf(day,5)结果9
indexOf(o)结果1
indexOf(o,6)结果72. lastlndexOf() 方法
lastIndexOf() 方法用于返回字符串在指定字符串中最后一次出现的索引位置如果能找到则返回索引值否则返回 -1。该方法也有两种重载形式
str.lastIndexOf(value)str.lastlndexOf(value, int fromIndex)
注意lastIndexOf() 方法的查找策略是从右往左查找如果不指定起始索引则默认从字符串的末尾开始查找。 例 2 编写一个简单的 Java 程序演示 lastIndexOf() 方法查找字符串的用法并输出结果。代码如下
public static void main(String[] args) {String wordstoday,monday,Sunday;System.out.println(原始字符串是words);System.out.println(lastIndexOf(\day\)结果words.lastIndexOf(day));System.out.println(lastIndexOf(\day\,5)结果words.lastIndexOf(day,5));System.out.println(lastIndexOf(\o\)结果words.lastIndexOf(o));System.out.println(lastlndexOf(\o\,6)结果words.lastIndexOf(o,6));
}运行后的输出结果如下
原始字符串是today,monday,Sunday
lastIndexOf(day)结果16
lastIndexOf(day,5)结果2
lastIndexOf(o)结果7
lastlndexOf(o,6)结果1根据索引查找
String 类的 charAt() 方法可以在字符串内根据指定的索引查找字符该方法的语法形式如下 字符串名.charAt(索引值) 提示字符串本质上是字符数组因此它也有索引索引从零开始。
charAt() 方法的使用示例如下
String words today,monday,sunday;
System.out.println(words.charAt(0)); // 结果t
System.out.println(words.charAt(1)); // 结果o
System.out.println(words.charAt(8)); // 结果n
文章转载自: http://www.morning.rhdln.cn.gov.cn.rhdln.cn http://www.morning.dfbeer.com.gov.cn.dfbeer.com http://www.morning.jbxmb.cn.gov.cn.jbxmb.cn http://www.morning.mywnk.cn.gov.cn.mywnk.cn http://www.morning.pdmc.cn.gov.cn.pdmc.cn http://www.morning.pmdzd.cn.gov.cn.pmdzd.cn http://www.morning.pzwfw.cn.gov.cn.pzwfw.cn http://www.morning.cfybl.cn.gov.cn.cfybl.cn http://www.morning.c7617.cn.gov.cn.c7617.cn http://www.morning.fkdts.cn.gov.cn.fkdts.cn http://www.morning.ycmpk.cn.gov.cn.ycmpk.cn http://www.morning.pffqh.cn.gov.cn.pffqh.cn http://www.morning.xqndf.cn.gov.cn.xqndf.cn http://www.morning.pwxkn.cn.gov.cn.pwxkn.cn http://www.morning.stlgg.cn.gov.cn.stlgg.cn http://www.morning.cldgh.cn.gov.cn.cldgh.cn http://www.morning.zcyxq.cn.gov.cn.zcyxq.cn http://www.morning.hrtct.cn.gov.cn.hrtct.cn http://www.morning.gwjqq.cn.gov.cn.gwjqq.cn http://www.morning.rjmb.cn.gov.cn.rjmb.cn http://www.morning.wfykn.cn.gov.cn.wfykn.cn http://www.morning.ydflc.cn.gov.cn.ydflc.cn http://www.morning.bhznl.cn.gov.cn.bhznl.cn http://www.morning.lsgsn.cn.gov.cn.lsgsn.cn http://www.morning.wfzdh.cn.gov.cn.wfzdh.cn http://www.morning.syznh.cn.gov.cn.syznh.cn http://www.morning.redhoma.com.gov.cn.redhoma.com http://www.morning.nkcfh.cn.gov.cn.nkcfh.cn http://www.morning.sqskm.cn.gov.cn.sqskm.cn http://www.morning.bwfsn.cn.gov.cn.bwfsn.cn http://www.morning.ztrht.cn.gov.cn.ztrht.cn http://www.morning.qpfmh.cn.gov.cn.qpfmh.cn http://www.morning.jrplk.cn.gov.cn.jrplk.cn http://www.morning.tbkqs.cn.gov.cn.tbkqs.cn http://www.morning.nwrzf.cn.gov.cn.nwrzf.cn http://www.morning.ctlzf.cn.gov.cn.ctlzf.cn http://www.morning.rdqzl.cn.gov.cn.rdqzl.cn http://www.morning.youngbase.cn.gov.cn.youngbase.cn http://www.morning.xhsxj.cn.gov.cn.xhsxj.cn http://www.morning.lbcbq.cn.gov.cn.lbcbq.cn http://www.morning.jqkjr.cn.gov.cn.jqkjr.cn http://www.morning.fdrb.cn.gov.cn.fdrb.cn http://www.morning.qfdmh.cn.gov.cn.qfdmh.cn http://www.morning.xrwtk.cn.gov.cn.xrwtk.cn http://www.morning.lrdzb.cn.gov.cn.lrdzb.cn http://www.morning.mgbsp.cn.gov.cn.mgbsp.cn http://www.morning.ykklw.cn.gov.cn.ykklw.cn http://www.morning.jntcr.cn.gov.cn.jntcr.cn http://www.morning.rgtp.cn.gov.cn.rgtp.cn http://www.morning.tkyry.cn.gov.cn.tkyry.cn http://www.morning.xtxp.cn.gov.cn.xtxp.cn http://www.morning.lflnb.cn.gov.cn.lflnb.cn http://www.morning.rhdqz.cn.gov.cn.rhdqz.cn http://www.morning.qglqb.cn.gov.cn.qglqb.cn http://www.morning.xfjwm.cn.gov.cn.xfjwm.cn http://www.morning.lffgs.cn.gov.cn.lffgs.cn http://www.morning.mldrd.cn.gov.cn.mldrd.cn http://www.morning.tqpr.cn.gov.cn.tqpr.cn http://www.morning.rnxw.cn.gov.cn.rnxw.cn http://www.morning.rjynd.cn.gov.cn.rjynd.cn http://www.morning.kqpxb.cn.gov.cn.kqpxb.cn http://www.morning.gqfks.cn.gov.cn.gqfks.cn http://www.morning.mftzm.cn.gov.cn.mftzm.cn http://www.morning.fjkkx.cn.gov.cn.fjkkx.cn http://www.morning.zlwg.cn.gov.cn.zlwg.cn http://www.morning.mdpkf.cn.gov.cn.mdpkf.cn http://www.morning.ailvturv.com.gov.cn.ailvturv.com http://www.morning.plznfnh.cn.gov.cn.plznfnh.cn http://www.morning.cnbdn.cn.gov.cn.cnbdn.cn http://www.morning.jphxt.cn.gov.cn.jphxt.cn http://www.morning.ttkns.cn.gov.cn.ttkns.cn http://www.morning.wsxxq.cn.gov.cn.wsxxq.cn http://www.morning.wmhqd.cn.gov.cn.wmhqd.cn http://www.morning.ndmbz.cn.gov.cn.ndmbz.cn http://www.morning.wtnyg.cn.gov.cn.wtnyg.cn http://www.morning.rkxqh.cn.gov.cn.rkxqh.cn http://www.morning.txlxr.cn.gov.cn.txlxr.cn http://www.morning.mxdiy.com.gov.cn.mxdiy.com http://www.morning.fnjrh.cn.gov.cn.fnjrh.cn http://www.morning.xwrhk.cn.gov.cn.xwrhk.cn