String相关
面试题
package com.atguigu.exer;
public class StringTest {
String str = new String("good");
char[] ch = {'t','e','s','t'};
public void change(String str,char ch[]){
str = "test ok";
ch[0] = 'b';
}
public static void main(String[] args) {
StringTest ex = new StringTest();
ex.change(ex.str,ex.ch);
System.out.println(ex.str);
System.out.println(ex.ch);
}
}
String与char[]和byte[]之间的转换
package com.atguigu.java;
import org.junit.Test;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
public class StringTest1 {
@Test
public void test4(){
String s1 = "javaEEhadoop";
String s2 = "javaEE";
String s3 = s2 + "hadoop";
System.out.println(s1 == s3);
final String s4 = "javaEE";
String s5 = s4 + "hadoop";
System.out.println(s1 == s5);
}
@Test
public void test2() throws UnsupportedEncodingException {
String str1 = "abc123中国";
byte[] bytes = str1.getBytes();
System.out.println(Arrays.toString(bytes));
byte[] gbks = str1.getBytes("gbk");
System.out.println(Arrays.toString(gbks));
System.out.println("****************************");
String str2 = new String(bytes);
System.out.println(str2);
String str3 = new String(gbks);
System.out.println(str3);
String str4 = new String(gbks, "gbk");
System.out.println(str4);
}
@Test
public void test1(){
String str1 = "abc123";
char[] charArray = str1.toCharArray();
for (int i = 0; i < charArray.length; i++) {
System.out.print(charArray[i] + " ");
}
System.out.println();
for (int i = 1; i < charArray.length / 2; i++) {
char c = charArray[i];
charArray[i] = charArray[charArray.length - 1 - i];
charArray[charArray.length - 1 - i] = c;
}
for (int i = 0; i < charArray.length; i++) {
System.out.print(charArray[i] + " ");
}
System.out.println();
String str3 = new String(charArray);
System.out.println(str3);
char[] arr = new char[]{'h','e','l','l','o'};
String str2 = new String(arr);
System.out.println(str2);
}
}
String的使用
package com.atguigu.java;
import org.junit.Test;
public class StringTest {
@Test
public void test1(){
String s1 = "abc";
String s2 = "abc";
s1 = "hello";
System.out.println(s1 == s2);
System.out.println(s1);
System.out.println(s2);
System.out.println("*******************************");
String s3 = "abc";
s3 += "def";
System.out.println(s3);
System.out.println(s2);
System.out.println("*******************************");
String s4 = "abc";
String s5 = s4.replace('a', 'm');
System.out.println(s4);
System.out.println(s5);
}
@Test
public void test2(){
String s1 = "javaEE";
String s2 = "javaEE";
String s3 = new String("javaEE");
String s4 = new String("javaEE");
System.out.println(s1 == s2);
System.out.println(s1 == s3);
System.out.println(s1 == s4);
System.out.println(s3 == s4);
Person p1 = new Person("Tom",12);
Person p2 = new Person("Tom",12);
System.out.println(p1.name.equals(p2.name));
System.out.println(p1.name == p2.name);
p1.name = "Jerry";
System.out.println(p2.name);
}
@Test
public void test3(){
String s1 = "javaEE";
String s2 = "hadoop";
String s3 = "javaEEhadoop";
String s4 = "javaEE" + "hadoop";
String s5 = s1 + "hadoop";
String s6 = "javaEE" + s2;
String s7 = s1 + s2;
System.out.println(s3 == s4);
System.out.println(s3 == s5);
System.out.println(s3 == s6);
System.out.println(s5 == s6);
System.out.println(s3 == s7);
System.out.println(s5 == s7);
System.out.println(s6 == s7);
String s8 = s5.intern();
System.out.println(s3 == s8);
}
}
String类的常用方法
package com.atguigu.java;
import org.junit.Test;
public class StringMethodTest {
@Test
public void test4(){
String str1 = "北京尚硅谷教育北京";
String str2 = str1.replace('北', '东');
System.out.println(str1);
System.out.println(str2);
String str3 = str1.replace("北京", "上海");
System.out.println(str3);
}
@Test
public void test1() {
String s1 = "helloworld";
System.out.println(s1.length());
System.out.println(s1.charAt(0));
System.out.println(s1.charAt(9));
System.out.println(s1.isEmpty());
String s2 = s1.toLowerCase();
System.out.println(s1);
System.out.println(s2);
String s5 = s1.toUpperCase();
System.out.println(s5);
String s3 = " he llo worl d ";
String s4 = s3.trim();
System.out.println("----" + s3 + "----");
System.out.println("----" + s4 + "----");
}
@Test
public void test2() {
String s1 = "HelloWorld";
String s2 = "helloworld";
System.out.println(s1.equals(s2));
System.out.println(s1.equalsIgnoreCase(s2));
String s3 = "abc";
String s4 = s3.concat("def");
System.out.println(s4);
String s5 = "abc";
String s6 = new String("abe");
System.out.println(s5.compareTo(s6));
String s7 = "北京尚硅谷教育";
String s8 = s7.substring(2);
System.out.println(s7);
System.out.println(s8);
String s9 = s7.substring(2, 5);
System.out.println(s9);
}
@Test
public void test3() {
String str1 = "helloworld";
boolean b1 = str1.endsWith("ld");
System.out.println(b1);
boolean b2 = str1.startsWith("h", 0);
System.out.println(b2);
boolean b3 = str1.startsWith("el", 1);
System.out.println(b3);
}
}
StringBuffer类的常用方法