Java 字符串表示不可变的字符序列,并且一旦创建就无法更改。 字符串的类型为java.lang.String
类。 在此页面中,学习有关使用字符串字面值和构造器,字符串方法以及与字符串转换和格式设置有关的各种字符串示例创建字符串的信息。
用 Java 创建字符串的方法有两种。
-
字符串字面值最简单,建议使用在 Java 中创建字符串。 这样,只需将双引号中的字符分配给
java.lang.String
类型的变量。String blogName = "howtodoinjava.com"; String welcomeMessage = "Hello World !!";
字符串字面值存储在字符串池中,字符串池是由 JVM 创建的特殊内存区域。 一个
String
只能有一个实例。 具有相同字符序列的任何第二个String
都将第一个字符串的引用存储在字符串池中。 它可以高效地使用字符串,并在运行时节省大量物理内存。String blogName1 = "howtodoinjava.com"; String blogName2 = "howtodoinjava.com"; String blogName3 = "howtodoinjava.com"; String blogName4 = "howtodoinjava.com"; String blogName5 = "howtodoinjava.com";
在上面的示例中,我们创建了 5 个具有相同
char
序列的字符串字面值。 在 JVM 内部,字符串池中只有一个String
实例。 所有其余的 4 个实例将共享为第一个字面值创建的字符串字面值的引用。 -
有时,我们可能希望为内存中的每个单独的字符串创建单独的实例。 我们可以使用新的关键字为每个字符串值创建一个字符串对象。
使用
new
关键字创建的字符串对象 – 存储在堆内存中。String blogName1 = new String("howtodoinjava.com"); String blogName2 = new String("howtodoinjava.com"); String blogName3 = new String("howtodoinjava.com");
在上面的示例中,堆内存中将有 3 个具有相同值的
String
实例。
-
char charAt(int index)
– 返回指定索引处的字符。 指定的索引值应介于0
至length() -1
之间(包括两个端点)。 如果索引无效/超出范围,则抛出IndexOutOfBoundsException
。String blogName = "howtodoinjava.com"; char c = blogName.charAt(5); //'d'
-
boolean equals(Object obj)
– 将字符串与指定的字符串进行比较,如果两者均匹配,则返回true
,否则返回false
。String blogName = "howtodoinjava.com"; blogName.equals( "howtodoinjava.com" ); //true blogName.equals( "example.com" ); //false
-
boolean equalsIgnoreCase(String str)
– 与equals
方法相同,但不区分大小写。String blogName = "howtodoinjava.com"; blogName.equalsIgnoreCase( "howtodoinjava.com" ); //true blogName.equalsIgnoreCase( "HowToDoInJava.com" ); //true
-
int compareTo(String string)
– 根据字符串中每个字符的 Unicode 值按字典顺序比较两个字符串。 您可以考虑基于字典的比较。如果参数字符串等于此字符串,则返回值为 0;否则,返回值为 0。 如果此字符串在字典上小于字符串参数,则小于 0 的值; 如果该字符串在字典上大于字符串参数,则该值大于 0。
String blogName = "howtodoinjava.com"; blogName.compareTo( "HowToDoInJava.com" ); //32 blogName.compareTo( "example.com" ); //3
-
int compareToIgnoreCase(String str)
– 与CompareTo
方法相同,但是在比较期间忽略大小写。String blogName = "howtodoinjava.com"; blogName.compareToIgnoreCase( "HowToDoInJava.com" ); //0 blogName.compareToIgnoreCase( "example.com" ); //3
-
boolean startsWith(String prefix, int offset)
– 从指定的偏移量索引开始,检查String
是否具有指定的前缀。String blogName = "howtodoinjava.com"; blogName.startsWith( "d", 5 ); //true blogName.startsWith( "e", 5 ); //false
-
boolean startsWith(String prefix)
– 测试字符串是否已指定prefix
,如果是,则返回true
,否则返回false
。 在此重载方法中,偏移索引值为 0。String blogName = "howtodoinjava.com"; blogName.startsWith( "h" ); //true blogName.startsWith( "e" ); //false
-
boolean endsWith(String subfix)
– 查字符串是否以指定的后缀结尾。String blogName = "howtodoinjava.com"; blogName.endsWith( "com" ); //true blogName.endsWith( "java" ); //false
-
int hashCode()
– 返回字符串的哈希码。String blogName = "howtodoinjava.com"; blogName.hashCode(); //1894145264
-
int indexOf(int ch)
– 返回指定字符参数在字符串中首次出现的索引。String blogName = "howtodoinjava.com"; blogName.indexOf( 'o' ); //1
-
int indexOf(int ch, int fromIndex)
–indexOf(char ch)
方法的重载版本,但是它开始从指定的fromIndex
中搜索字符串。String blogName = "howtodoinjava.com"; blogName.indexOf( 'o', 5 ); //6
-
int indexOf(String str)
– 返回指定子字符串str
首次出现的索引。String blogName = "howtodoinjava.com"; blogName.indexOf( "java" ); //9
-
int indexOf(String str, int fromIndex)
-indexOf(String str)
方法的重载版本,但是它开始从指定的fromIndex
中搜索字符串。String blogName = "howtodoinjava.com"; blogName.indexOf( "java" , 5); //9
-
int lastIndexOf(int ch)
– 返回字符串中字符'ch'
的最后一次出现。String blogName = "howtodoinjava.com"; blogName.lastIndexOf( 'o' ); //15
-
int lastIndexOf(int ch,int fromIndex)
–lastIndexOf(int ch)
方法的重载版本。 从fromIndex
开始向后搜索。String blogName = "howtodoinjava.com"; blogName.lastIndexOf( 'o', 5 ); //4
-
int lastIndexOf(String str)
– 返回最后一次出现的字符串str
的索引。 与lastIndexOf(int ch)
相似。String blogName = "howtodoinjava.com"; blogName.lastIndexOf( "java" ); //9
-
int lastIndexOf(String str, int fromIndex)
–lastIndexOf(String str)
方法的重载版本。 从fromIndex
开始向后搜索。String blogName = "howtodoinjava.com"; blogName.lastIndexOf( "java", 6 ); //9
-
String substring(int beginIndex)
– 返回字符串的子字符串。 子字符串以指定索引处的字符开头。String blogName = "howtodoinjava.com"; blogName.substring( 7 ); //injava.com
-
String substring(int beginIndex, int endIndex)
– 返回子字符串。 子字符串以beginIndex
处的字符开头,以endIndex
处的字符结尾。String blogName = "howtodoinjava.com"; blogName.substring( 7, 9 ); //in
-
String concat(String str)
– 在字符串的末尾连接指定的字符串参数。String blogName = "howtodoinjava.com"; blogName.concat( " Hello Visitor !!" ); //howtodoinjava.com Hello Visitor !!
-
String replace(char oldChar, char newChar)
– 使用newChar
参数更改所有出现的oldChar
之后,返回新的更新字符串。String blogName = "howtodoinjava.com"; blogName.replace( 'o', 'O' ); //hOwtOdOinjava.cOm
-
public String replace(CharSequence target, CharSequence replacement)
– 使用replacement
参数更改所有出现的target
后,返回新的更新字符串。String blogName = "howtodoinjava.com"; blogName.replace( "com", "COM" ); //howtodoinjava.COM
-
String replaceFirst(String regex, String replacement)
– 用指定的替换字符串替换与给定正则表达式参数匹配的子字符串的第一个匹配项。String blogName = "howtodoinjava.com"; blogName.replaceFirst("how", "HOW"); //HOWtodoinjava.com
-
String.replaceAll(String regex, String replacement)
– 用替换字符串替换所有出现的与正则表达式参数匹配的子字符串。 -
String[] split(String regex, int limit)
– 拆分字符串并返回与给定正则表达式匹配的子字符串数组。limit
是数组中元素的最大数量。String blogName = "howtodoinjava.com"; blogName.split("o", 3); //[h, wt, doinjava.com]
-
String[] split(String regex)
– 先前方法的重载,没有任何阈值限制。 -
boolean contains(CharSequence s)
– 检查字符串是否包含指定的char
值序列。 如果是,则返回true
,否则返回false
。 如果参数为null
,则抛出NullPointerException
。String blogName = "howtodoinjava.com"; blogName.contains( "java" ); //true blogName.contains( "python" ); //false
-
public String toUpperCase(Locale locale)
– 使用指定语言环境定义的规则将字符串转换为大写字符串。String blogName = "howtodoinjava.com"; blogName.toUpperCase( Locale.getDefault() ); //HOWTODOINJAVA.COM
-
String.toUpperCase()
– 先前的toUpperCase()
方法的重载版本,带有默认语言环境。 -
String toLowerCase(Locale locale)
– 使用给定语言环境定义的规则将字符串转换为小写字符串。 -
String.toLowerCase()
– 具有默认语言环境的先前方法的重载版本。 -
String.intern()
– 在内存池中搜索指定的字符串,如果找到,则返回它的引用。 否则,此方法将在字符串池中分配创建字符串字面值并返回引用。 -
boolean isEmpty()
– 如果给定的字符串长度为 0,则返回true
,否则返回false
。String blogName = "howtodoinjava.com"; blogName.isEmpty(); //false "".isEmpty(); //true
-
static String join()
- 使用指定的分隔符连接给定的字符串,并返回连接的 JavaString
字面值。String.join("-", "how","to", "do", "in", "java") //how-to-do-in-java
-
static String format()
– 返回格式化的字符串。 -
String.trim()
- 从 Java 字符串中删除开头和结尾的空格。 -
char[] toCharArray()
– 将字符串转换为字符数组。 -
static String copyValueOf(char[] data)
– 返回一个字符串,其中包含指定字符数组的字符。char[] chars = new char[] {'h','o','w'}; String.copyValueOf(chars); //how
-
byte[] getBytes(String charsetName)
– 使用指定的字符集编码将字符串转换为字节序列。 -
byte [] getBytes()
– 先前方法的重载版本。 它使用默认字符集编码。 -
int length()
– 返回字符串的长度。 -
boolean match(String regex)
– 验证字符串是否与指定的正则表达式参数匹配。 -
int codePointAt(int index)
– 与charAt()
方法相似。 它返回指定索引的 Unicode 代码点值,而不是字符本身。 -
static String copyValueOf(char[] data, int offset, int count)
– 先前方法的重载版本,带有两个额外的参数 – 子数组的初始偏移量和子数组的长度。 它根据额外的参数从数组中选择字符,然后创建字符串。 -
getChars(int srcBegin, int srcEnd, char [] dest, int destBegin)
– 将src
数组的字符复制到dest
数组。 仅将指定范围复制(从srcBegin
到srcEnd
)到dest
子数组(从destBegin
开始)。 -
static String valueOf()
– 返回所传递参数的字符串表示形式,例如int
,long
,float
,double
,char
和char
数组。 -
boolean contentEquals(StringBuffer sb)
– 将字符串与指定的字符串缓冲区进行比较。 -
boolean regionMatches(int srcoffset, String dest, int destoffset, int len)
– 将输入的子字符串与指定字符串的子字符串进行比较。 -
boolean regionMatches(boolean ignoreCase, int srcoffset, String dest, int destoffset, int len)
–regionMatches
方法的另一个变体,带有额外的布尔值参数,用于指定比较是区分大小写还是不区分大小写。
- 将 Java 字符串转换为
int
- 在 Java 中将
int
转换为字符串 - 将字符串转换为长
- 在 Java 中将
Long
转换为字符串 - 将字符串转换为日期
- 将日期转换为字符串
- 将字符串转换为
String[]
示例 - Java 8 – 连接字符串数组 – 将数组转换为字符串
- 将字符串转换为
InputStream
示例 - 将
InputStream
转换为字符串示例 - Java 拆分 CSV 字符串 – 将字符串转换为列表示例
- 将 CSV 连接到字符串
- 将 HTML 转义为字符串示例
- 转义 HTML – 将字符串编码为 HTML 示例
- 将字节数组转换为字符串
StackTrace
到字符串的转换- 将浮点数转换为字符串 – 格式转换为 N 个小数点