资讯专栏INFORMATION COLUMN

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

xiaokai / 1556人阅读

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

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

</>复制代码

  1. char c3 = "a";
  2. int i = c3+2;
  3. System.out.println(i); //99
  4. char c4 = (char) i;
  5. System.out.println(c4); //c
不可变字符序列:String String类

String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象

java把String类声明的final类,不能有类

String类对象创建后不能修改,由0或多个字符组成,包含在一对双引号之间

public final class String

</>复制代码

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

public String(char value[]) {

</>复制代码

  1. this.value = Arrays.copyOf(value, value.length);
  2. }

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常用方法

</>复制代码

  1. public class Demo1 {
  2. // String 对象的创建
  3. public static void test1() {
  4. String s = "apple";
  5. String s1 = new String();
  6. String s2 = new String("apple");
  7. String s3 = new String(s2);
  8. byte[] bytes = new byte[] {1,2,3};
  9. String s4 = new String(bytes);
  10. char[] chars = { "a", "b", "c", "d", "e","f" };
  11. String s5 = new String(chars); // abcdef
  12. String s6 = new String(chars, 1, 4); //bcde
  13. }
  14. // 常用方法
  15. public static void test2(){
  16. String s1 = "apple";
  17. String s2 = " a,p,p l e ";
  18. System.out.println(s1+s2); //字符串连接
  19. System.out.println(s1.length()); //字符串长度
  20. System.out.println(s1.charAt(2)); // 获取指定未知的字符
  21. char[] charArray = s1.toCharArray(); //转换为char[]
  22. System.out.println(s1.substring(1, 3)); //截取字符串
  23. String[] ss = s2.split(","); // 用,分割字符串
  24. for (String s : ss) {
  25. System.out.println(s);
  26. }
  27. System.out.println(s2.trim()); //去掉两端的空格
  28. if (s1.equals(s2)) { //字符串比较
  29. System.out.println(s1+"=="+s2);
  30. }else {
  31. System.out.println(s1+"!="+s2);
  32. }
  33. String s3 = "java is a good computer language";
  34. System.out.println(s3.indexOf("o")); //用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。
  35. System.out.println(s3.indexOf("o",13)); // 从13开始向右查找
  36. System.out.println(s3.indexOf("good")); //查找字符串
  37. System.out.println(s3.lastIndexOf("o")); //从右往左查找
  38. System.out.println(s3.toUpperCase()); //转大写字母
  39. System.out.println(s3.startsWith("a")); //s3是否以a开头
  40. System.out.println(s3.contains("good")); //s3是否包含"good"
  41. }
  42. // 字符串与基本类型的转换
  43. public static void test3(){
  44. //字符类型转为基本类型
  45. System.out.println(Integer.parseInt("123"));
  46. System.out.println(Double.parseDouble("123.4"));
  47. System.out.println(Float.parseFloat("123.456f"));
  48. // 基本类型转为字符类型
  49. System.out.println(String.valueOf(123.4));
  50. //进制转换
  51. String binaryString = Long.toBinaryString(123); //二进制
  52. String octalString = Long.toOctalString(123); //8
  53. String hexString = Long.toHexString(123); //16
  54. String string = Long.toString(123,16); //任意进制
  55. System.out.println(binaryString);
  56. System.out.println(octalString);
  57. System.out.println(hexString);
  58. System.out.println(string);
  59. }
  60. public static void main(String[] args) {
  61. test2();
  62. test3();
  63. }
  64. }
可变字符序列:StringBuilder、StringBuffer StringBuilder(线程不安全,效率高)

</>复制代码

  1. public final class StringBuilder extends AbstractStringBuilder
  2. implements java.io.Serializable, CharSequence{
  3. public StringBuilder() {
  4. super(16);
  5. }
  6. public StringBuilder(String str) {
  7. super(str.length() + 16);
  8. append(str );
  9. }
  10. public AbstractStringBuilder append(String str ) {
  11. if (str == null)
  12. return appendNull();
  13. int len = str.length();
  14. ensureCapacityInternal(count + len);
  15. str.getChars(0, len, value, count);
  16. count += len;
  17. return this;
  18. }
  19. private void ensureCapacityInternal(int minimumCapacity) {
  20. // overflow-conscious code
  21. if (minimumCapacity - value.length > 0)
  22. expandCapacity(minimumCapacity);
  23. }
  24. void expandCapacity(int minimumCapacity) { //数组扩容
  25. int newCapacity = value .length * 2 + 2;
  26. if (newCapacity - minimumCapacity < 0)
  27. newCapacity = minimumCapacity;
  28. if (newCapacity < 0) {
  29. if (minimumCapacity < 0) // overflow
  30. throw new OutOfMemoryError();
  31. newCapacity = Integer.MAX_VALUE;
  32. }
  33. value = Arrays.copyOf(value, newCapacity);
  34. }
StringBuffer(线程安全,效率低)

</>复制代码

  1. public final class StringBuffer
  2. extends AbstractStringBuilder
  3. implements java.io.Serializable, CharSequence
  4. {
  5. @Override
  6. public synchronized int length() {
  7. return count;
  8. }
  9. @Override
  10. public synchronized int capacity() {
  11. return value.length;
  12. }
使用

</>复制代码

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

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

转载请注明本文地址: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元查看
<