indexOf()/equals/contains


indexOf():对大小写敏感
定义:返回某个指定字符串值在字符串中首次出现位置
用法:返回字符中indexof(string)中字串string在父串中首次出现的位置,从0开始!没有返回-1;方便判断和截取字符串!
语法:stringObject.indexOf(searchvalue,formindex)
searchvalue  必须,规定需检索的字符串值
formindex  可选的整数参数。规定在字符串中开始检索的位置。它的合法取值是0到 - 1。如省略该参数,则将从字符串的首字符开始检索
equals和contains
equals是判断字符串的是不是相等

例如:
   "abc".equals("abc"),返回结果是Boolean类型的true
   "abc".equals("ab")返回结果是Boolean类型的false
contains是包含的意思
例如:
  "abc".contains("a"),返回结果是Boolean类型的true
  "abc".contains("d"),返回结果是Boolean类型的false.
contains包含包括,"abc".contains("abcd")是返回的false

equals和==
String s1,s2,s3,s4;
s1=new String("we are students");  //new是新建一个对象
s2="We are students";          //常量池
s3="We are students";          //常量池
s4=new String(s1);          //new新建一个对象
//s2/s3都是指向常量池中的"We are students",所以是相同的
//s1/s4虽然内容一样,但却是不同的对象

System.out.println(s1.equals(s2));     //true
System.out.println(s3==s2);        //true
System.out.println(s1.equals(s4));   //true
System.out.println(s1==s4);      //false
作者:howtosay
         
放牛娃的个人笔记整理,每天记录一点点,进步一点点
原文地址:https://www.cnblogs.com/hongzm/p/7864940.html