Java知识点总结(常用类-字符类)

Java知识点总结(常用类-字符类)

@(Java知识点总结)[Java, Java常用类]

[toc]

Char

  1. char类型用来比奥斯在Unicode编码中的字符。Unicode用来处理各种语言的所有文字,它占2个字节,0~65535。
  2. 单引号用来表示字符常量,表示一个字符,它与"a"不同,”a"表示含有一个字符的字符串。

    char c1 = 'a';

  3. Java语言中还允许使用转义字符 ‘’ ,来将其后的字符转义为其他的含义

    char c2 = 'n'; //代表换行符

  4. char是在0~65535范围,运算时直接当作整数来运算。可以把0~65535直接的整数直接转型为char
 char c3 = 'a';  
 int  i = c3+2;
 System.out.println(i); //99
 char  c4 = (char) i;
 System.out.println(c4); //c

不可变字符序列:String

String类
  • String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象
  • java把String类声明的final类,不能有类
  • String类对象创建后不能修改,由0或多个字符组成,包含在一对双引号之间

    public final class String

    implements java.io.Serializable, Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final char value[]; //声明一个不可变数组用来存放字符
    public String(String original) {
        this.value = original.value;
        this.hash = original.hash;
    }

    public String(char value[]) {

        this.value = Arrays.copyOf(value, value.length);
    }
    
String类构造方法
水果价格
public String()无参构造方法,用来创建空字符串的String对象
public String(String str)用已知的字符串str创建一个String对象
public String(char[] chars)用字符数组chars创建一个String对象
public String(byte[] bytes)用byte数组bytes创建一个String对象
public String(char[] chars, int startIndex, int numChars)用字符数组chars的startIndex开始的numChars个字符创建一个String对象
public String(StringBuffer buffer)用StringBuffer 创建一个String对象
String常用方法
public class Demo1 {
 
  // String 对象的创建
  public static void test1()  {
  
   String s = "apple";
   
   String s1 = new String();  
   
   String s2 = new String("apple");
   
   String s3 = new String(s2);
   
   byte[] bytes = new byte[] {1,2,3};
   String s4 = new String(bytes);
   
   char[] chars = { 'a', 'b', 'c', 'd', 'e','f' };
   String s5 = new String(chars); // abcdef
   String s6 = new String(chars, 1, 4);  //bcde
                   
  }
  // 常用方法
  public static void test2(){
   
   String s1 = "apple";
   String s2 = "  a,p,p l e  ";
   
   System.out.println(s1+s2); //字符串连接
   
   System.out.println(s1.length()); //字符串长度
   
   System.out.println(s1.charAt(2)); // 获取指定未知的字符
   
   char[] charArray = s1.toCharArray(); //转换为char[]
   
   System.out.println(s1.substring(1, 3)); //截取字符串
   
   String[] ss = s2.split(","); // 用,分割字符串
   for (String s : ss) {
     System.out.println(s);
   }
  
   System.out.println(s2.trim()); //去掉两端的空格
  
   if (s1.equals(s2)) {  //字符串比较
     System.out.println(s1+"=="+s2);
   }else {
     System.out.println(s1+"!="+s2);
   }
   
   String s3 = "java is a good computer language";
   System.out.println(s3.indexOf("o"));  //用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。
   System.out.println(s3.indexOf("o",13)); // 从13开始向右查找
   System.out.println(s3.indexOf("good"));  //查找字符串
   System.out.println(s3.lastIndexOf("o"));  //从右往左查找
   
   System.out.println(s3.toUpperCase()); //转大写字母
   
   System.out.println(s3.startsWith("a")); //s3是否以a开头
   
   System.out.println(s3.contains("good")); //s3是否包含"good"
  }
 
  // 字符串与基本类型的转换
  public static void test3(){
   
   //字符类型转为基本类型
   System.out.println(Integer.parseInt("123"));
   System.out.println(Double.parseDouble("123.4"));
   System.out.println(Float.parseFloat("123.456f"));
   
   // 基本类型转为字符类型
   System.out.println(String.valueOf(123.4));
   
   //进制转换
   String binaryString = Long.toBinaryString(123); //二进制
   String octalString = Long.toOctalString(123); //8
   String hexString = Long.toHexString(123); //16
   String string = Long.toString(123,16); //任意进制
    System.out.println(binaryString);
    System.out.println(octalString);
    System.out.println(hexString);
    System.out.println(string);
  }
  public static void main(String[] args) {
 
    test2();
    test3();
  }
 
}

可变字符序列:StringBuilder、StringBuffer

StringBuilder(线程不安全,效率高)
public final class StringBuilder  extends AbstractStringBuilder
                    implements java.io.Serializable, CharSequence{
 
public  StringBuilder() {
        super(16);
    }
public  StringBuilder(String str) {
        super(str.length() + 16);
        append(str );
    }
 public AbstractStringBuilder append(String str ) {
        if (str == null)
            return appendNull();
        int len = str.length();
         ensureCapacityInternal(count  + len); 
        str.getChars(0, len, value, count);
        count += len;
        return this;
    }
private  void ensureCapacityInternal(int minimumCapacity) {
        // overflow-conscious code
        if (minimumCapacity - value.length > 0)
            expandCapacity(minimumCapacity);
    }
void  expandCapacity(int minimumCapacity) { //数组扩容
        int newCapacity = value .length  * 2 + 2;
        if (newCapacity - minimumCapacity < 0)
            newCapacity = minimumCapacity;
        if (newCapacity < 0) {
            if (minimumCapacity < 0) // overflow
                throw new OutOfMemoryError();
            newCapacity = Integer.MAX_VALUE;
        }
        value = Arrays.copyOf(value, newCapacity);
    }

StringBuffer(线程安全,效率低)
public final class StringBuffer
    extends AbstractStringBuilder
    implements java.io.Serializable, CharSequence
{
    @Override
    public synchronized  int length() {
        return count;
    }
 
    @Override
    public synchronized  int capacity() {
        return value.length;
    }

使用

clipboard.png

public class Demo2 {
  public static void main(String[] args) {
    test2();
  }
 
  // 新建对象
  private static void test1() {
    StringBuffer sb1 = new StringBuffer();
    StringBuffer sb2 = new StringBuffer(20);
    StringBuffer sb3 = new StringBuffer("apple");
  }
  
  // 常用方法
  private static void test2() {
    StringBuffer sb = new StringBuffer();
    sb.append("java"); //字符串表示形式追加到序列
    sb.append("is a good computer language");
    System.out.println(sb.toString()); //转换为string
    
    System.out.println(sb.charAt(11)); //返回此序列中指定索引处的 char 值
    
    sb.delete(3, 15); //移除此序列的子字符串中的字符
    System.out.println(sb.toString());
    
    char[] chars = new char[20];
    sb.getChars(4, 11,chars, 0);  //将字符从此序列复制到目标字符数组  dst。
    System.out.println(new String(chars));
    
    System.out.println(sb.charAt(9)); //返回第一次出现的指定子字符串在该字符串中的索引。
    
    System.out.println(sb.insert(2, "6666").toString()); //将字符串插入此字符序列中。
    
    System.out.println(sb.replace(2, 6, "888")); //使用给定 String 中的字符替换此序列的子字符串中的字符。
    
    System.out.println(sb.reverse()); //将此字符序列用其反转形式取代。
    
    System.out.println(sb.lastIndexOf("8")); //返回最右边出现的指定子字符串在此字符串中的索引。
    
    System.out.println(sb.substring(5,15).toString()); //返回一个新的字符序列,该字符序列是此序列的子序列。
    
    System.out.println(sb.length()); //返回长度
  }
  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值