勒索做钓鱼网站的人/百度软文推广怎么做
**
代码来源牛客网,当作学习笔记用,侵删
**
查找字符串最后出现位置
以下实例中我们通过字符串函数 strOrig.lastIndexOf(Stringname) 来查找子字符串 Stringname 在 strOrig 出现的位置:
public class SearchlastString {public static void main(String[] args) {String strOrig = "Hello world ,Hello Nowcoder";int lastIndex = strOrig.lastIndexOf("Nowcoder");if(lastIndex == - 1){System.out.println("没有找到字符串 Nowcoder");}else{System.out.println("Nowcoder 字符串最后出现的位置: "+ lastIndex);}}
}
以上代码实例输出结果为:
Nowcoder 字符串最后出现的位置: 19
删除字符串中的一个字符
public class Main {public static void main(String args[]) {String str = "this is Java";System.out.println(removeCharAt(str, 3));}public static String removeCharAt(String s, int pos) {return s.substring(0, pos) + s.substring(pos + 1);}
}
以上代码实例输出结果为:
thi is Java
字符串替换
public class StringReplaceEmp{public static void main(String args[]){String str="Hello World";System.out.println( str.replace( 'H','W' ) );System.out.println( str.replaceFirst("He", "Wa") );System.out.println( str.replaceAll("He", "Ha") );}
}
以上代码实例输出结果为:
Wello World
Wallo World
Hallo World
字符串反转
以下实例演示了如何使用 Java 的反转函数 reverse() 将字符串反转:
public class StringReverseExample{public static void main(String[] args){String string="nowcoder";String reverse = new StringBuffer(string).reverse().toString();System.out.println("字符串反转前:"+string);System.out.println("字符串反转后:"+reverse);}
}
以上代码实例输出结果为:
字符串反转前:nowcoder
字符串反转后:redocwon
字符串搜索
以下实例使用了 String 类的 indexOf() 方法在字符串中查找子字符串出现的位置,如果存在返回字符串出现的位置(第一位为0),如果不存在返回 -1:
public class SearchStringEmp {public static void main(String[] args) {String strOrig = "Google Nowcoder Taobao";int intIndex = strOrig.indexOf("Nowcoder");if(intIndex == - 1){System.out.println("没有找到字符串 Nowcoder");}else{System.out.println("Nowcoder 字符串位置 " + intIndex);}}
}
以上代码实例输出结果为:
Nowcoder 字符串位置 7