String常见知识点api

String类

1.常见构造方法

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):把字符数组的一部分转成字符串
public String(String original):把字符串常量值转成字符串
String s1 = new String();
System.out.println(s1);  //空字符

//GET请求中文乱码解决**
**String newStr =  new String("带有中文变量".getBytes("UTF-8"),"ISO8859-1")**

byte[] arr1 = {97,98,99};		
String s2 = new String(arr1);			//解码,将计算机读的懂的转换成我们读的懂
System.out.println(s2); //abc

//a, b, c , d , e , f  对应ascii码
byte[] arr2 = {97,98,99,100,101,102};
String s3 = new String(arr2,2,3);		//将arr2字节数组从2索引开始转换3个
System.out.println(s3); // cde

char[] arr3 = {'a','b','c','d','e'};	//将字符数组转换成字符串
String s4 = new String(arr3);
System.out.println(s4);  //abcde

String s5 = new String(arr3,1,3);		//将arr3字符数组,从1索引开始转换3个
System.out.println(s5);  //bcd

String s6 = new String("java");
System.out.println(s6);  //java


2.String类的常见面试题

	@Test
		public void test3() throws Exception {
			String str = "abc";					//"abc"可以看成一个字符串对象
			str = "def";						//当把"def"赋值给str,原来的"abc"就变成了垃圾
			System.out.println(str);
		}
@Test
		public void test5() throws Exception {
			byte b = 3 + 4;             //在编译时就变成7.然后把赋值给b  常量优化机制
			String s1 = "a" + "b" +"c"; //
			String s2 = "abc";
			System.out.println(s1 == s2); //true java中有常量优化机制	
			System.out.println(s1.equals(s2));
			
		}
		
		@Test
		public void test6() throws Exception {
			String s1 = new String("abc");  //记录的是堆内存对象的地址值
			String s2 = "abc";              //记录的是常量池的地址值
			System.out.println(s1 == s2);  //false
			System.out.println(s1.equals(s2)); //true
		}
		
		@Test
		public void test7() throws Exception {
			//创建几个对象
			//创建两个对象,一个在常量池中,一个在堆内存中
			String s1 = new String("abc");		
			System.out.println(s1);
		}
		
		@Test
		public void test8() throws Exception {
			//常量池中没有这个字符串对象,就创建一个,如果有直接用即可
			String s1 = "abc";
			String s2 = "abc";
			System.out.println(s1 == s2); 			//true	
			System.out.println(s1.equals(s2)); 		//true
		}
	
		@Test
		public void test10() throws Exception {
			 String s1 = "ab";
			 String s2 = "abc";
			 String s3 = s1 + "c";
			 System.out.println(s3 == s2); //false
			 System.out.println(s3.equals(s2));  //true

3.其他api

//String类的判断功能
/*boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
boolean contains(String str):判断大字符串中是否包含小字符串
boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
boolean isEmpty():判断字符串是否为空。int length():获取字符串的长度。*/
		 @Test
		public void test13() throws Exception {
		 	String s1 = "heima";
			String s2 = "";
			String s3 = null;
			System.out.println(s1.isEmpty());  // false
			System.out.println(s2.isEmpty()); //true
			System.out.println(s3.isEmpty()); //NullPointerException
		}

//String类的获取功能
/*char charAt(int index):获取指定索引位置的字符
int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。如果此没有这样的字符,则返回 -1,下同
int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引.*/
		@Test
		public void testIndex() throws Exception {
			String s1 = "heima";
			int index = s1.indexOf('e'); //1
			System.out.println(index);
			String s2 = "heima";
			int index2 = s1.indexOf('z'); //-1
			System.out.println(index2);
			String s = "woaiheima";
			s.substring(4);		//subString会产生一个新额字符串,需要将新的字符串记录
			System.out.println(s); //woaiheima
		}
/* String  contains(String str) 拼接字符串
 int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次       出现处的索引。
 lastIndexOf  返回指定字符在此字符串中最后一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。*/

String substring(int start):从指定位置开始截取字符串,默认到末尾。
String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。范围左闭右开 "[ )" 
"abcde".substring(0,3)  // abc  
    
  //String的转换功能
/*byte[] getBytes():把字符串转换为字节数组。
  char[] toCharArray():把字符串转换为字符数组。
  static String valueOf(char[] chs):把字符数组转成字符串。
  static String valueOf(int i):把int类型的数据转成字符串。
 *注意:String类的valueOf方法可以把任意类型的数据转成字符串*/
 
  /* A:String的替换功能及案例演示
    String replace(char old,char new)
    String replace(String old,String new)
    String replaceAll(String regex,String new)*/
    
    String newStr1 = "com.jd.".replaceAll("." , "/")+"Myclass.class";
    String newStr2 = "com.jd.".replace('.' , '/')+"Myclass.class";
    System.out.println(newStr1);  //   ///Myclass.class
    System.out.println(newStr2);  //  com/jd/Myclass.class	
 //因为replaceAll的第一个参数为正则表达式 "."在正则中匹配全部

/*B:String的去除字符串两空格及案例演示
     String trim()
 C:String的按字典顺序比较两个字符串及案例演示
    int compareTo(String str)
    int compareToIgnoreCase(String str)*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值