Java String charAt():
此方法返回特定索引的字符值. 索引号从 0 开始, 以 n-1 结束, 其中 n 是字符串的长度. 在给定索引大于或等于字符串长度或为负数时, 该方法返回 StringIndexOutOfBoundsException.
- public class CharAtTest {
- public static void main(String args[]) {
- String st = "Java String";
- char result = s.charAt(10);
- System.out.println(result);
- }
- }
Java String compareTo()
此方法根据字符串中每个字符的 Unicode 值按字典顺序比较字符串的值. 然后它返回一个整数值, 报告该字符串是否大于, 等于或小于第二个字符串. 当第一个字符串大于第二个字符串时, 它返回一个正数. 当第一个字符串小于第二个字符串时, 它返回一个负数. 在两个字符串相等的情况下, 该方法返回 0.
- public class CompareToDemo {
- public static void main(String args[]) {
- String st1 = "Hello";
- String st2 = "World";
- System.out.println(st1.compareTo(st2));
- }
- }
Java String concat
string concat() 方法通过将特定字符串连接到当前字符串的末尾来创建新字符串. 然后它返回组合的字符串.
- public class ConcatTest {
- public static void main(String args[]) {
- String st = "Hello";
- st = st.concat("World");
- System.out.println(st);
- }
- }
Java String contains()
当在字符串中找到需要的字符序列值时, 返回 true, 否则返回 false.
- class ContainsTest {
- public static void main(String args[]) {
- String name = "Hello";
- System.out.println(name.contains("World"));
- System.out.println(name.contains("Java"));
- System.out.println(name.contains("Community"));
- }
- }
Java String endsWith()
如果字符串以特定后缀结尾, 返回 true, 否则返回 false.
- class EndsWithTest {
- public static void main(String args[]) {
- String St = new String("Hello World Java Community");
- boolean obj;
- obj = St.endsWith("Hello World");
- System.out.println("First value =" + obj);
- obj = St.endsWith("Hello");
- System.out.println("Second Value =" + obj);
- }
- }
Java String equals()
该方法根据字符串的内容对两个字符串进行比较. 如果两个字符串的所有字符都相同, 则返回 true, 否则返回 false. 它覆盖对象类的 equals() 方法.
- class EqualsTest {
- public static void main(String args[]) {
- String s1 = "JavaFolder";
- String s2 = "JavaFolder";
- String s3 = "javafolder";
- String s4 = "JAVAFOLDER";
- System.out.println(s1.equals(s2));
- System.out.println(s1.equals(s3));
- System.out.println(s1.equals(s4));
- }
- }
Java String equalsIgnoreCase()
此方法与 equals() 方法是相同的功能, 如果两个字符串的所有字符匹配, 则返回 true. 但是, 此方法只检查字符串的内容, 而不检查大小写.
- class EqualsIgnoreCaseTest {
- public static void main(String[] args) {
- String st1 = "Hello";
- String st2 = "World";
- String st3 = "Hello World";
- boolean first = st2.equalsIgnoreCase(st1);
- boolean second = st2.equalsIgnoreCase(st3);
- System.out.println(first);
- System.out.println(second);
- }
- }
Java String format()
此方法按特定格式, 区域设置和参数返回格式化字符串. 它类似于 C 语言中的 sprint() 函数和 printf() 方法. 在程序员未在此方法中指定语言环境的情况下, 将 Locale.getDefault() 方法作为缺省值调用.
- public class FormatTest {
- public static void main(String[] args) {
- String st = String.format("First: %1$d Second: %2$d Third: %3$d", 100, 200, 300);
- System.out.println(st);
- }
- }
Java String getBytes()
此方法返回字符串的字节序列. 换句话说, 它返回字符串的字节数组.
- public class GetBytesTest {
- public static void main(String[] args) {
- String st = "PQRSXYZ";
- byte[] bt = st.getBytes();
- for (int i = 0; i < bt.length; i++) {
- System.out.println(bt[i]);
- }
- }
- }
Java String getChars()
此方法不返回任何值, 但会将字符串的内容复制到特定的字符数组中. 此方法传递四个参数, 并且在 beginIndex 大于 endIndex 的情况下, 会抛出 StringIndexOutOfBoundsException 异常.
- public class StringGetCharsTest {
- public static void main(String args[]) {
- String str = new String("Hello World");
- char[] obj = new char[10];
- try {
- str.getChars(3, 5, obj, 1);
- System.out.println(obj);
- } catch (Exception ex) {
- System.out.println(ex);
- }
- }
Java String indexOf()
该方法返回 substring 的特定 char 值的最后一个索引. 与 indexOf() 方法一样, index 的计数器从 0 开始, 当字符串中找不到 char 值时, 该方法也返回 - 1. 在 Java 中, lastIndexOf 方法的类型如下:
int lastIndexOf(int ch):
这个方法返回特定字符值的最后一个索引的位置.
int lastIndexOf(int ch,int fromIndex):
这个方法返回索引中特定字符值的最后一个索引的位置.
int lastIndexOf(String substring):
这个方法返回截取字符串后的最后一个索引的位置.
int lastIndexOf(String substring,int fromIndex):
这个方法返回索引中截取字符串后的最后一个索引的位置.
- public class LastIndexOfTest {
- public static void main(String args[]) {
- String st1 = "Hello Java World";
- int obj = st1.lastIndexOf('J');
- System.out.println(obj);
- }
- }
Java String length()
此方法返回字符串中存在的字符总数. Java 字符串的长度与它的 Unicode 单位完全相同.
- public class LengthTest {
- public static void main(String args[]) {
- String st1 = "HelloJava";
- String st2 = "World";
- System.out.println("string length:" + st1.length());
- System.out.println("string length :" + st2.length());
- }
- }
未完待续....
参考: https://www.javafolder.com/java-string-methods
感谢你花时间读到结尾!:D
来源: https://juejin.im/post/5bc40a565188255ca153a560