资讯专栏INFORMATION COLUMN

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

xiaokai / 1374人阅读

摘要:知识点总结常用类字符类知识点总结常用类类型用来比奥斯在编码中的字符。使用给定中的字符替换此序列的子字符串中的字符。将此字符序列用其反转形式取代。返回最右边出现的指定子字符串在此字符串中的索引。

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

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

[toc]

Char

char类型用来比奥斯在Unicode编码中的字符。Unicode用来处理各种语言的所有文字,它占2个字节,0~65535。

单引号用来表示字符常量,表示一个字符,它与"a"不同,”a"表示含有一个字符的字符串。

char c1 = "a";

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

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

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, 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;
    }

使用

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()); //返回长度
  }
  
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/69139.html

相关文章

  • Java识点总结常用-包装

    摘要:知识点总结常用类包装类知识点总结常用类包装类是一个面向对象的语言,但是中的基本数据类型却不是面向对象的。但是我们在实际使用中经常将基本数据类型转换成对象,便于操作。 Java知识点总结(常用类-包装类) @(Java知识点总结)[Java, Java常用类] [toc] 包装类(wrapper) Java是一个面向对象的语言,但是Java中的基本数据类型却不是面向对象的。但是我们在实际...

    AlanKeene 评论0 收藏0
  • Java识点总结常用-Date

    摘要:知识点总结常用类类知识点总结常用类在标准类库中包含一个类。它的对象表示一个特定的瞬间,精确到毫秒。中时间的表示说白了也是数字,是从标准纪元点开始到某个时刻的毫秒数,类型是。 Java知识点总结(常用类-Date类) @(Java知识点总结)[Java, Java常用类] [toc] 在标准Java类库中包含一个Date类。它的对象表示一个特定的瞬间,精确到毫秒。 Java中时间的表...

    Kyxy 评论0 收藏0
  • Java学习路线总结,搬砖工逆袭Java架构师(全网最强)

    摘要:哪吒社区技能树打卡打卡贴函数式接口简介领域优质创作者哪吒公众号作者架构师奋斗者扫描主页左侧二维码,加入群聊,一起学习一起进步欢迎点赞收藏留言前情提要无意间听到领导们的谈话,现在公司的现状是码农太多,但能独立带队的人太少,简而言之,不缺干 ? 哪吒社区Java技能树打卡 【打卡贴 day2...

    Scorpion 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<