字符串是字符序列,在Java编程中广泛使用,在Java编程语言中,字符串被视为对象 ,Java平台提供了String字符串类来创建和操作字符串。
创建字符串
创建字符串的最直接方法是编写-
String greeting = "Hello world!";
每当在代码中遇到字符串文字时,编译器都会创建一个String 对象 ,其值在这种情况下为“ Hello world!”。
public class StringDemo {
public static void main(String args[]) {
char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' };
String helloString = new String(helloArray);
System.out.println( helloString );
}
}
这将产生以下输出-
hello.
如果有必要对字符串进行大量修改,则应使用字符串缓冲区StringBuffer 或StringBuilder
字符串长度
信息的方法称为访问器方法,length方法它返回String 对象中包含的字符数。
以下程序是length,方法字符串类的示例。
public class StringDemo {
public static void main(String args[]) {
String palindrome = "Dot saw I was Tod";
int len = palindrome.length();
System.out.println( "String length is : " + len );
}
}
这将产生以下输出-
String length is : 17
连接字符串
String类包括一个用于连接两个字符串-的方法
string1.concat(string2);
这将返回一个新字符串,该字符串的末尾添加了string2,您还可以将concat方法与字符串文字一起使用
"My name is ".concat("Zara");
字符串更常与+运算符连接在一起,如在-中
"Hello," + " world" + "!"
输出
"Hello, world!"
让无涯教程看看下面的示例-
public class StringDemo {
public static void main(String args[]) {
String string1 = "saw I was ";
System.out.println("Dot " + string1 + "Tod");
}
}
这将产生以下输出-
Dot saw I was Tod
格式化字符串
使用String的static format方法可以创建可重用的格式化字符串,而不是一次性的print语句。
System.out.printf("The value of the float variable is " +
"%f, while the value of the integer " +
"variable is %d, and the string " +
"is %s", floatVar, intVar, stringVar);
你可以写成-
String fs;
fs = String.format("The value of the float variable is " +
"%f, while the value of the integer " +
"variable is %d, and the string " +
"is %s", floatVar, intVar, stringVar);
System.out.println(fs);
字符串方法
下面是String类-支持的方法列表
Sr.No. | Method & Remark |
---|
1 |
char charAt(int index) 返回指定索引处的字符。 |
2 |
int compareTo(Object o) 将此字符串与另一个对象进行比较。 |
3 |
int compareTo(String anotherString) 按词典顺序比较两个字符串。 |
4 |
int compareToIgnoreCase(String str) 按词典顺序比较两个字符串,忽略大小写。 |
5 |
String concat(String str) 将指定的字符串连接到此字符串的末尾。 |
6 |
boolean contentEquals(StringBuffer sb) 当且仅当此字符串表示与指定的StringBuffer相同的字符序列时,才返回true。 |
7 |
static String copyValueOf(char[] data) 返回一个字符串,该字符串表示指定数组中的字符序列。 |
8 |
static String copyValueOf(char[] data, int offset, int count) 返回一个字符串,该字符串表示指定数组中的字符序列。 |
9 |
boolean endsWith(String suffix) 判断此字符串是否以指定的后缀结束。 |
10 |
boolean equals(Object anObject) 将此字符串与指定的对象进行比较。 |
11 |
boolean equalsIgnoreCase(String anotherString) 将此字符串与另一个字符串进行比较,忽略大小写。 |
12 |
byte getBytes() 使用平台的默认字符集将此字符串编码为字节序列,并将输出存储到新的字节数组中。 |
13 |
byte[] getBytes(String charsetName) 使用命名字符集将此字符串编码为字节序列,并将输出存储到新的字节数组中。 |
14 |
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 将此字符串中的字符复制到目标字符数组中。 |
15 |
int hashCode() 返回此字符串的哈希代码。 |
16 |
int indexOf(int ch) 返回指定字符的第一个匹配项在此字符串中的索引。 |
17 |
int indexOf(int ch, int fromIndex) 返回此字符串中指定字符的第一个匹配项的索引,从指定索引处开始搜索。 |
18 |
int indexOf(String str) 返回指定子字符串的第一个匹配项在此字符串内的索引。 |
19 |
int indexOf(String str, int fromIndex) 返回此字符串中指定子字符串的第一个匹配项的索引,从指定的索引处开始。 |
20 |
String intern() 返回String对象的规范表示形式。 |
21 |
int lastIndexOf(int ch) 返回指定字符的最后一个匹配项在此字符串中的索引。 |
22 |
int lastIndexOf(int ch, int fromIndex) 返回指定字符的最后一个匹配项在此字符串中的索引,从指定索引处开始向后搜索。 |
23 |
int lastIndexOf(String str) 返回指定子字符串最右侧出现的字符串内的索引。 |
24 |
int lastIndexOf(String str, int fromIndex) 返回指定子字符串的最后一个匹配项在此字符串中的索引,从指定索引处开始向后搜索。 |
25 |
int length() 返回此字符串的长度。 |
26 |
boolean matches(String regex) 指示此字符串是否与给定的正则表达式匹配。 |
27 |
boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) 判断两个字符串区域是否相等。 |
28 |
boolean regionMatches(int toffset, String other, int ooffset, int len) 判断两个字符串区域是否相等。 |
29 |
String replace(char oldChar, char newChar) 返回一个新字符串,该字符串是通过用newChar替换此字符串中所有oldChar的匹配项而产生的。 |
30 |
String replaceAll(String regex, String replacement) 将此字符串中与给定正则表达式匹配的每个子字符串替换为给定替换。 |
31 |
String replaceFirst(String regex, String replacement) 用给定的替换与给定正则表达式匹配的此字符串的第一个子字符串。 无涯教程网 |
32 |
String[] split(String regex) 在给定正则表达式的匹配拆分此字符串。 |
33 |
String[] split(String regex, int limit) 在给定正则表达式的匹配拆分此字符串。 |
34 |
boolean startsWith(String prefix) 判断此字符串是否以指定的前缀开头。 |
35 |
boolean startsWith(String prefix, int toffset) 判断此字符串是否以指定的前缀开始指定的索引。 |
36 |
CharSequence subSequence(int beginIndex, int endIndex) 返回一个新的字符序列,它是此序列的子序列。 |
37 |
String substring(int beginIndex) 返回作为此字符串的子字符串的新字符串。 |
38 |
String substring(int beginIndex, int endIndex) 返回作为此字符串的子字符串的新字符串。 |
39 |
char[] toCharArray() 将此字符串转换为新的字符数组。 |
40 |
String toLowerCase() 使用默认区域设置的规则将此字符串中的所有字符转换为小写。 链接:https://www.learnfk.comhttps://www.learnfk.com/java/java-strings.html 来源:LearnFk无涯教程网 |
41 |
String toLowerCase(Locale locale) 使用给定区域设置的规则将此字符串中的所有字符转换为小写。 |
42 |
String toString() 返回字符串内容。 |
43 |
String toUpperCase() 使用默认区域设置的规则将此字符串中的所有字符转换为大写。 |
44 |
String toUpperCase(Locale locale) 使用给定区域设置的规则将此字符串中的所有字符转换为大写。 |
45 |
String trim() 省略前面和后面的空格。 |
46 |
static String valueOf(primitive data type x) 返回传递的数据类型参数的字符串表示形式。 |