method
Meaning |
|
String ()
Create a string with empty content |
|
Stirng ( String value)
Create an object based on the specified string content |
|
String (char[] value)
Create an object based on the specified character array |
|
Give a chestnut:
public static void main(String[] args){
String s1 =new String();//创建空字符串
String s2 = new String("张三")//创建一个内容为张三的字符串
char[] charArray = new char[]{'A','B','C'};
String s3 = new String(charArray);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
Commonly used methods of the Sring class:
Basic string Operation:
public static void main(String[] args) {
String s ="abcdefhckoj";//声明字符串
System.out.println("字符串的长度为:"+s.length());//获取字符串长度, 即字符个数
System.out.println("字符串第一个字符:"+s.charAt(0));
System.out.println("字符c第一次出现的位置:"+s.indexOf('c'));
System.out.println("字符c最后一次出现的位置:"+s.lastIndexOf('c'));
}
Conversion between string and array:
public class Demo{
public static void main(String[] args){
String s = "abcdesgs";//声明一个字符串
System.out.println("将此字符串装换为数组");
char [] charArray = s.toCharArray();//字符串转换为字符数组。
System.out.println("[);
for(int i=0;i,charArray.length;i++){
if(i !=charArray.length-1);
Syetem.out.println(charArray[i]+",");
//判断是否是数组最后一个元素,如果不是则在后面添逗号码
}else{
//否则 数组最后一个元素不加逗号。
Syetem.out.println(charArray[i]+"]");
}
}
Syetem.out.println("将Int 类型转换为String 类型:" +String.valueOf(20));
}
}
String judgment operation:
public static void main(String[] args) {
String str1 = "abcdsg";//声明一个字符串
String str2 = "adc";
System.out.println("判断是否以字符串abc开头:"+str1.startsWith("abc"));
System.out.println("判断是否以字符串sg结尾:"+str1.endsWith("sg"));
System.out.println("判断是否包含字符串ds:"+str2.contains("ds"));
System.out.println("判断是否为空:"+str1.isEmpty());
System.out.println("判断两个字符串是否相等"+str1.equals(str2));
Interception and segmentation of strings
String class provides interception and segmentation of strings Two methods, among which, the substring() method is used to intercept a part of the string, and the split() method can split the string according to a certain character.
package com.project.demo;
public class Test {
public static void main(String[] args) {
String string = "张三-李四-王五";
//截取字符串
System.out.println("从第4个字符截取到最后:"+string.substring(3));
System.out.println("从第3个字符截取到第6:"+string.substring(2,6));
// 字符串分割
System.out.println("分割后的字符串数组元素依次是: ");
String[] stringArray =string.split("-");//通过- 字符为标志,将其分割为字符串数组
for(int i =0;i<stringArray.length;i++) {
if(i != stringArray.length-1 ) {
System.out.println(stringArray[i]+",");
}else {
System.out.println(stringArray[i]);
System.out.println(string.length());
}
}
}
}
Remove spaces and replace operations:
package com.project.demo;
public class Test {
public static void main(String[] args) {
String string = " hello word ";
//字符串替换操作
System.out.println("将word 替换成java :"+string.replace("word", "java"));
//字符串去空操作
System.out.println("去除字符串两端的空格:"+ string.trim());
System.out.println("去除字符串中所有空格: "+string.replace(" ", ""));
System.out.println("将小写转换为大写 : " + string.toUpperCase());
}
}
One thing to note is that String is taking a certain character When accessing a character in a string, the index of the character (array subscript index) will be used. If the index of the character does not exist, StringIndexOutOfBoundsException(String subscript out-of-bounds exception# will occur) ##)
StringBuffer:
In order to facilitate the modification of strings, JDK provides a StringBuffer class (also called a character buffer). The biggest difference between the StringBuffer class and the String class is that its length and content are variable. StringBuffer is similar to a string container. , when adding or deleting characters, no new StringBuffer object will be generated.
Let’s take a look at the common methods of StringBuffer in the API:
During interviews we are often asked about the difference between String, StringBuffer and StringBuilder?? Briefly speaking, String string constant StringBuffer string variable (thread-safe) StringBuilder string variable (non-thread-safe) The main performance difference between the String type and the StringBuffer type is that String is an immutable object , so every time the String type is changed, it is actually equivalent to generating a new String object, and then pointing the pointer to the new String object. Therefore, it is best not to use String for strings that frequently change content, because each time it is generated Objects will have an impact on system performance. Especially when there are too many unreferenced objects in the memory, the JVM's GC will start to work, and the speed will definitely be quite slow.
If you use the StringBuffer class, each result will operate on the StringBuffer object itself, instead of generating a new object and then changing the object reference. So in general we recommend using StringBuffer, especially when string objects change frequently. In some special cases, the string concatenation of String objects is actually interpreted by the JVM as the concatenation of StringBuffer objects, so in these cases the speed of String objects will not be slower than that of StringBuffer objects, and especially the following string objects are generated Among them, String efficiency is much faster than StringBuffer: String S1 = “This is only a” + “ simple” + “ test”;
StringBuffer Sb = new StringBuilder("This is only a").append("simple").append("test"); You will be very happy I was surprised to find that the speed of generating String S1 objects is simply too fast, and at this time StringBuffer actually has no advantage in speed at all. In fact, this is a trick of the JVM. In the eyes of the JVM, this String S1 = “This is only a” “simple” “test”; is actually: String S1 = “This is only a simple test”; So of course there is no need to Much time. But what should be noted here is that if your string comes from another String object, the speed will not be that fast, for example: String S2 = “This is only a”; String S3 = “ simple”;
String S4 = “ test”;
String S1 = S2 +S3 + S4;
这时候 JVM 会规规矩矩的按照原来的方式去做 在大部分情况下 StringBuffer > String StringBuffer Java.lang.StringBuffer线程安全的可变字符序列。一个类似于 String 的字符串缓冲区,但不能修改。虽然在任意时间点上它都包含某种特定的字符序列,但通过某些方法调用可以改变该序列的长度和内容。 可将字符串缓冲区安全地用于多个线程。可以在必要时对这些方法进行同步,因此任意特定实例上的所有操作就好像是以串行顺序发生的,该顺序与所涉及的每个线程进行的方法调用顺序一致。 StringBuffer 上的主要操作是 append 和 insert 方法,可重载这些方法,以接受任意类型的数据。每个方法都能有效地将给定的数据转换成字符串,然后将该字符串的字符追加或插入到字符串缓冲区中。append 方法始终将这些字符添加到缓冲区的末端;而 insert 方法则在指定的点添加字符。
例如,如果 z 引用一个当前内容是“start”的字符串缓冲区对象,则此方法调用 z.append("le") 会使字符串缓冲区包含“startle”,而 z.insert(4, "le") 将更改字符串缓冲区,使之包含“starlet”。
在大部分情况下 StringBuilder > StringBuffer java.lang.StringBuilde java.lang.StringBuilder一个可变的字符序列是5.0新增的。此类提供一个与 StringBuffer 兼容的 API,但不保证同步。该类被设计用作 StringBuffer 的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍)。如果可能,建议优先采用该类,因为在大多数实现中,它比 StringBuffer 要快。两者的方法基本相同。
基本数据类型包装类:
何为包装类?
在java.lang包中有各种原生类相对应的类, 称作数值包装类。 这些类的名称, 大部分均以各原生类类型第一个字母换成大写的方式来命名 。简单的说就是将数据装换为对象,来应用更多的方法。
boolean |
Boolean |
byte |
Byte |
short |
Short |
int |
Integer |
long |
Long |
char |
Chracter |
float |
Float |
double |
Double |
包装类和基本数据类型在进行转换时,引入了装箱和拆箱的概念,其中装箱是指将基本数据类型的值转为引用数据类型, 反之, 拆箱是指将引用数据类型的对象转为基本数据类型。
举个栗子:
方式一: 通过new关键字
Integer in = new Integer(5);
方式二: 直接赋值
Integer in = 5;
构造函数可将原生数据类型数值或代表数值的字符串转换成一数值包装对象。
如: 在Double类中的构造函数
Double(double value)
Double(String s)
例:
Double tax=Double(8332349823.234);
在数值包装类中所定义的方法, 最常用到的就是一些用来做字符串与数值间转换的方法。比如:
static float parseFloat(String s)
static Float valueof(String s)
static String toString(float f)
在使用包装类的时候我们需要注意一些问题.
1、 包装类都重写了Object类中的toString()方法,以字符串的形式返回被包装的基本数据类型的值;
2、 除了Character外, 包装类都有parseXXX(String s)的静态方法,将字符串转换为对应的基本类型的数据。 参数s不能为null, 而且同样必须是可以解析为相应基本类型的数据,否则虽然编译通过,但运行时会报错;
3、 除了Character外,包装类都有valueOf(String s)方法,可以根据String类型的参数创建包装类对象, 但参数字符串s不能为null,而且字符串必须是可以解析为相应基本类型的数据,否则虽然编译通过, 但运行时也会报错