java推断字符串是不是包括指定字符串【JAVA教程】,java,字符串
indexOf(String s)的运用,假如包括,返回的值是包括该子字符串在父类字符串中肇端位置;假如不包括一定悉数返回值为-1
package my_automation; public class z_test { public static void main(String[] args) { String test = "This is test for string"; System.out.println(test.indexOf("This")); //0 System.out.println(test.indexOf("is")); //2 System.out.println(test.indexOf("test")); //8 System.out.println(test.indexOf("for")); //13 System.out.println(test.indexOf("for string "));//-1 if (test.indexOf("This")!=-1){ //"只需test.indexOf('This')返回的值不是-1申明test字符串中包括字符串'This',相反假如包括返回的值一定是-1" System.out.println("存在包括关联,由于返回的值不即是-1"); }else{ System.out.println("不存在包括关联,由于返回的值即是-1"); } if (test.indexOf("this")!=-1){ //"只需test.indexOf('this')返回的值不是-1申明test字符串中包括字符串'this',相反假如包括返回的值一定是-1" System.out.println("存在包括关联,由于返回的值不即是-1"); }else{ System.out.println("不存在包括关联,由于返回的值即是-1"); } } }
indexOf()的用法:
返回字符中indexof(string)中字串string在父串中初次涌现的位置,从0入手下手!没有返回-1;轻易推断和截取字符串!
indexOf()定义和用法indexOf() 要领可返回某个指定的字符串值在字符串中初次涌现的位置。
语法stringObject.indexOf(searchvalue,fromindex)
参数 形貌searchvalue 必须。划定需检索的字符串值。
fromindex 可选的整数参数。划定在字符串中入手下手检索的位置。它的正当取值是 0到 - 1。如省略该参数,则将从字符串的首字符入手下手检索。
更多java学问请关注java基础教程栏目。
以上就是java推断字符串是不是包括指定字符串的细致内容,更多请关注ki4网别的相干文章!