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);
}

qq 891451702
原文地址:https://www.cnblogs.com/duoyan/p/11793160.html