java 判断字符串是否包含特定的字符(截取指定字符前面或者后面的值)

public static void main(String[] args) {
  String str = "四川省成都市";
  if(str.indexOf("省")!=-1){
    System.out.println("存在");
  }else{
    System.out.println("不存在!");
  }
}

 

String address = "四川省—成都市";

String province = b.substring(0, b.indexOf("—"));

String city = b.substring(b.indexOf("—")+1);

System.out.println(province);//四川省

System.out.println(city);//成都市

 

也可以截取

public static void main(String[] args) {
  String str = "四川省成都市";
  String s = str.substring(0,3);
  if("四川省".equals(s)){
    System.out.println("存在");
  }else{
    System.out.println("不存在");
  }
}

 

 

public static void main(String[] args) {
String str = "某某保险股份有限公司机动车出境综合某某条款和费率的批复";
String s = str.substring(str.indexOf("司")+1,str.indexOf("条"));
System.out.println(s);
}

posted on 2019-11-04 17:19  多言  阅读(5195)  评论(0编辑  收藏  举报

导航