Home > Java > Java Tutorial > body text

Introduction to string related knowledge in java (code example)

不言
Release: 2019-02-16 13:57:29
forward
2120 people have browsed it

This article brings you some knowledge about string in Java (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Two creation methods

 String str1 = "abc";        //字面量创建
 String str2 = new String("abc");  //构造方法创建
Copy after login

When created using literals, only one object will be generated, while when created through the constructor method, two objects will be generated (str2 in the front and new String in the back) Object)

2. Common construction methods

public String()    //空参构造
public String(byte[] bytes)  //把字节数组转换成字符串
public String(byte[] bytes,int index,int length)  //把字节数组的一部分转换成字符串
public String(char[] value)  //把字符数组转换成字符串
public String(char[] value,int index,int count)  //把字符数组的一部分转换成字符串
Copy after login

3. Other common methods

int length()  //返回字符串长度
String substring(int beginIndex,int endIndex)      //获取字符串的一部分
//beginIndex 开始位置下标  endIndex 结束位置下标+1
String substring(int beginIndex)  //获取字符串的一部分
//beginIndex 开始位置下标 一直到最后
boolean startsWith(String prefix)  //判断一个字符串的开头、前缀是不是prefix
boolean endsWith(String prefix)  //判断一个字符串的结尾、后缀是不是prefix
boolean contains(String s)  //判断一个字符串是否包含s
int indexOf(char ch)  //查找一个字符,返回在字符串中第一次出现的索引,如果没找到,则返回-1
byte[] getBytes()  //将字符串转换成字节数组
char[] toCharArray()  //将字符串转换成字符数组
boolean equals(Object obj)  //判断字符串中的字符是否完全相同,完全相同返回true,区分大小写
boolean equalsIgnoreCase(String s)  //同上,忽略大小写
toUpperCase()  //转大写
toLowerCase()  //转小写
Copy after login

4. String, StringBuffer, StringBuilder

4.1. String is an immutable character sequence, and the length is determined when it is defined. StringBuffer and StringBuilder are variable character sequences. The default length is 16 bits. The positions less than 16 bits are empty when defined. They can be filled in later to achieve variable length.

4.2. StringBuffer is the jdk1.0 version, thread-safe and low-efficiency. StringBuilder is the jdk1.5 version, thread-safe and high-efficiency

The above is the detailed content of Introduction to string related knowledge in java (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!