此方法返回特定索引的字符值。 索引号从0开始,以 n-1结束,其中 n 是字符串的长度。 在给定索引大于或等于字符串长度或为负数时,该方法返回 StringIndexOutOfBoundsException。java
public class CharAtTest {
public static void main(String args[]) {
String st = "Java String";
char result = s.charAt(10);
System.out.println(result);
}
}
复制代码
此方法根据字符串中每一个字符的Unicode值按字典顺序比较字符串的值。 而后它返回一个整数值,报告该字符串是否大于,等于或小于第二个字符串。 当第一个字符串大于第二个字符串时,它返回一个正数。 当第一个字符串小于第二个字符串时,它返回一个负数。 在两个字符串相等的状况下,该方法返回0。程序员
public class CompareToDemo {
public static void main(String args[]) {
String st1 = "Hello";
String st2 = "World";
System.out.println(st1.compareTo(st2));
}
}
复制代码
string concat()方法经过将特定字符串链接到当前字符串的末尾来建立新字符串。 而后它返回组合的字符串。后端
public class ConcatTest {
public static void main(String args[]) {
String st = "Hello";
st = st.concat(" World");
System.out.println(st);
}
}
复制代码
当在字符串中找到须要的字符序列值时,返回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"));
}
}
复制代码
若是字符串以特定后缀结尾,返回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);
}
}
复制代码
该方法根据字符串的内容对两个字符串进行比较。若是两个字符串的全部字符都相同,则返回true,不然返回false。它覆盖对象类的equals()方法。spa
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));
}
}
复制代码
此方法与equals()方法是相同的功能,若是两个字符串的全部字符匹配,则返回true。可是,此方法只检查字符串的内容,而不检查大小写。code
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);
}
}
复制代码
此方法按特定格式,区域设置和参数返回格式化字符串。 它相似于C语言中的sprint()函数和printf()方法。 在程序员未在此方法中指定语言环境的状况下,将Locale.getDefault()方法做为缺省值调用。orm
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);
}
}
复制代码
此方法返回字符串的字节序列。换句话说,它返回字符串的字节数组。cdn
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]);
}
}
}
复制代码
此方法不返回任何值,但会将字符串的内容复制到特定的字符数组中。 此方法传递四个参数,而且在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);
}
}
复制代码
该方法返回substring的特定char值的最后一个索引。与indexOf()方法同样,index的计数器从0开始,当字符串中找不到char值时,该方法也返回-1。在Java中,lastIndexOf方法的类型以下:
public class LastIndexOfTest {
public static void main(String args[]) {
String st1 = "Hello Java World";
int obj = st1.lastIndexOf('J');
System.out.println(obj);
}
}
复制代码
此方法返回字符串中存在的字符总数。 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());
}
}
复制代码
未完待续....
感谢你花时间读到结尾!:D
后端一枚,默默搬砖撸代码,若是以为不错欢迎关注个人公众号