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());
}
}
复制代码
未完待续....