Java basic notes (5) Basic data type packaging class, String class

1. Packaging classes of basic data types

  • Byte
  • Short
  • Integer
  • Long
  • Character
  • Float
  • Double
  • Boolean
    puts the basic data types into the packaging process, boxing --> has a lot of new functions
    int --> Integer The process of
    converting packaging classes to basic data types, unboxing --> return to the basic data type
    Integer --> int
    JDK 1.5 requires manual boxing and unboxing before
  1. Manual packing XXXValue()
  2. Overload method, requiring that the parameter string can only contain numbers. If it contains others, NumberFormatException will occur.
    JDK1.5 can automatically box and unbox
public class Test {
    
    
	public static void main(String[] args) {
    
    
		int i = 3;
		System.out.println(Integer.MIN_VALUE);	
		  // 1. 手动装箱
		Integer integer = Integer.valueOf(7);
		Integer integer2 = Integer.valueOf("123");
		System.out.println(integer2);		
		Integer integer3 = new Integer(6);		
		//手动拆箱
		int i4 = integer.intValue();
		System.out.println(i4);
		byte b = integer2.byteValue();
		  short s = integer2.shortValue();
		  long l = integer2.longValue();
		  float f = integer2.floatValue();
		  double d = integer2.doubleValue();	
		  // 自动装箱	  		  
		  Integer integer4 = 5;
		  System.out.println(integer4);
		  //自动拆箱
		  int i2 = integer4;
		  System.out.println(i2);		  
		  Byte b2 = 3;
		  Double double1 = 4.5;	  
		  byte b3 = b2;
	}
}

static boolean isLetter(char ch)
determines whether the specified character is a letter
isLowerCase(char ch)
determines whether the specified character is a lowercase letter.
isUpperCase(char ch)
determines whether the specified character is an uppercase letter.
static char toLowerCase(char ch)
uses the case mapping information from the UnicodeData file to convert character parameters to lowercase.
static char toUpperCase(char ch)
uses the case mapping information from the UnicodeData file to convert character parameters to uppercase

public class Test3 {
    
    
	public static void main(String[] args) {
    
    
		Character c = '2';
		char c2 = c;		
		// 字母: 各个国家常用的文字		
		System.out.println("是否是字母"+Character.isLetter('好'));		
		System.out.println("判断是否是大写"+Character.isUpperCase('a'));
		System.out.println("判断是否是小写"+Character.isLowerCase('C'));		
		System.out.println("转成大写"+Character.toUpperCase('a'));
		System.out.println("转成小写"+Character.toLowerCase('B'));
	}
}

2. The final class of String , the
content is not allowed to be modified. Once modified, a new object will be generated.
String actually uses a character array to help store data

import java.util.Arrays;
public class Test {
    
    
	public static void main(String[] args) {
    
    
		String str = "abc";
		String str2 = new String("abc");
		// 有很多方法是利用了下标的
		// StringIndexOutOfBoundsException 字符串下标越界
		char c = str.charAt(0);
		System.out.println(c);		
		//将字符串转成数组
		char[] cs = str.toCharArray();
		System.out.println(Arrays.toString(cs));		
		byte[] bytes = str.getBytes();
		System.out.println(Arrays.toString(bytes));		
	//  将数组换成 字符串
		byte[] bs = new byte[]{
    
    98,99,100};
		String str3 = new String(bs);
		System.out.println(str3);
		String str4 = new String(bs, 1, 2);// offset 从下标为几开始转, length 转几个    下标+length<=数组的长度
		System.out.println(str4);		
		char[] cs2 = new char[]{
    
    'a','b','c'};
		String string = new String(cs2, 1, 2);
		System.out.println(string);		
		String string2 = new String();
		System.out.println(string2);// ""
	}
}

Judging method
equals Judging whether the contents of two strings are the same
contains Judging whether the string contains the specified string
startsWith Whether the string starts with the specified string
endsWith Whether the string ends with the specified string

public class Test2 {
    
    
	public static void main(String[] args) {
    
    
		String string = "a"+"b";
		String string2 = string.concat("cdeccc");// 拼接字符串
		System.out.println(string2);		
		// 判断方法
		// equals  判断两个字符串内容是否相同
		// contains 判断字符串中是否包含指定的字符串
		boolean b = string2.contains("adb");
		System.out.println(b);
		// startsWith  字符串是否以指定的字符串开头
		boolean b2 = string2.startsWith("abcde");
		System.out.println(b2);
		// endsWith     字符串是否以指定的字符串结尾
		boolean b3 = string2.endsWith("cde");
		System.out.println(b3);		
		// 查找参数在调用者字符串中第一次出现的下标
		int index = string2.indexOf("c");
		System.out.println(index);
		 查找参数在调用者字符串中最后一次出现的下标
		int index2 = string2.lastIndexOf("c");
		System.out.println(index2);		
		// 转成大写
		String upperCase = string2.toUpperCase();
		System.out.println(upperCase);
		// 转成小写
		String lowerCase = upperCase.toLowerCase();
		System.out.println(lowerCase);		
		// length 得到字符串的长度
		System.out.println(lowerCase.length());		
		// 去除字符串中前边和后面空白
		lowerCase = "  a b  c  ";
		String string3 = lowerCase.trim();
		System.out.println(string3);
	}
}

boolean matches(String regex)
tells whether this string matches the given regular expression.
String replace(char oldChar, char newChar)
returns a new string, which is obtained by replacing all oldChar that appears in this string with newChar.
String replace(CharSequence target, CharSequence replacement)
uses the specified literal replacement sequence to replace all substrings of this string that match the literal target sequence.
String replaceAll(String regex, String replacement)
uses the given replacement to replace all substrings of this string that match the given regular expression.
String replaceFirst(String regex, String replacement)
uses the given replacement to replace the first substring that matches the given regular expression.
String[] split(String regex)
splits this string according to the match of the given regular expression.
String substring(int beginIndex)
returns a new string, which is a substring of this string.
String substring(int beginIndex, int endIndex)
returns a new string, which is a substring of this string.

public class Test3 {
    
    
	public static void main(String[] args) {
    
    
		String  str = "abcaaabdaabaa#def#ff#hhh";
		String string = str.replace("#", "?");//replace("#", "?"); 将所有符合条件的字符串全部替换, 不支持正则表达式
		System.out.println(string);		
		// 只替换第一个符合正则表达式的部分
		String first = str.replaceFirst("[abcdef]+", "?");
		System.out.println(first);
		// 替换所有符合正则表达式的部分
		String all = str.replaceAll("[abcdef]+", "?");
		System.out.println(all);		
		// 字母 数字 下划线     6 ~8位
		// 手机号  11位数
		// 身份证号 18位 
		String string2 = "aaaa";
		boolean matches = string2.matches("[a]+");
		System.out.println(matches);		
		// subString 截取
		String string3 = "abcdefg";
	}
}

Guess you like

Origin blog.csdn.net/Echoxxxxx/article/details/112444735