字符串

String

字符串是引用数据类型,但是它的值在创建后不可以更改。

String str = "abc";
//这两种表达方式是等价的
char data[] = {'a', 'b', 'c'};
String str = new String(data);

两种简单的构造方法

//public String(String original)
//用旧字符串,初始化新创建的String对象
String str = "abc";
String str_1 = new String(str);
System.out.println(str.equals(str_1));//结果为true
//public String(char[] value)
//分配一个新的String ,以便它表示当前包含在字符数组参数中的字符序列,字符数组的内容被复制。
char[] c = {'a','b','c'};
String str = new String(c);
System.out.println(str);//结果为 abc

常用的字符串方法

  • length isEmpty
  • public char charAt(int index) 返回char指定索引处的值
  • public int codePointAt(int index) 该处字符对应的ASCII码值或unicode值
  • public void getChars(int srcBegin, int srcEnd,char[] dst, int dstBegin)方法将字符从字符串复制到目标字符数组。
        String str = "abcde";
		char[] c = new char[10];
		str.getChars(0, 3, c, 0);
		for(char s:c) {
			System.out.println(s);//a b c null.......
		}
  • public String concat(String str)
    将指定的字符串连接到该字符串的末尾。
  • public boolean equals(Object anObject) 当且仅当该参数不是null并且是String对象,字符内容相同时返回true
  • public boolean equalsIgnoreCase(String anotherString) 忽略大小写的比较
  • public int compareTo(String anotherString)
    从左向右依次比较两个字符串对象的内容。字符不同则返回字符差值;字符相同依次向后比较,直到字符不同或者字符串结束。字符相同则返回长度差。差是左减右,字符差是ascll差值。相同返回0.
  • public boolean startsWith(String prefix)
  • public boolean endsWith(String suffix)
  • 获取索引
    • public int indexOf(int ch) 返回指定字符第一次出现的字符串内的索引。
    • public int indexOf(int ch,int fromIndex)从指定索引处向后搜索,再作判定。
    • public int lastIndexOf(int ch)返回指定字符的最后一次出现的字符串中的索引。
    • public int lastIndexOf(int ch,int fromIndex) 从指定索引处向后搜索,再作判定。
      同样的,方法参数还可以是字符串对象。
  • public String substring(int beginIndex,int endIndex)
    返回一个字符串,该字符串是此字符串的子字符串。 子串开始于指定beginIndex并延伸到字符索引endIndex - 1 。
  • public String concat(String str) 将指定的字符串连接到该字符串的末尾。
  • public String replace(char oldChar,char newChar) 替换字符串字符
  • public boolean matches(String regex) true/false
  • public boolean contains(CharSequence s)
String str1 = "abd";
System.out.println(str1.contains("ab"));//true
//CharSequence与String都能用于定义字符串,但CharSequence的值是可读可写序列
  • public String replaceAll(String regex, String replacement)
    成功则返回替换的字符串,失败则返回原始字符串。
    例子来源
public class Test {
    public static void main(String args[]) {
        String Str = new String("www.google.com");
        System.out.print("匹配成功返回值 :" );
        System.out.println(Str.replaceAll("(.*)google(.*)", "runoob" ));//runoob
        System.out.print("匹配失败返回值 :" );
        System.out.println(Str.replaceAll("(.*)taobao(.*)", "runoob" ));//www.google.com
    }
}
  • public String replace(CharSequence target,CharSequence replacement)
  • split
  • join
  • public char[] toCharArray() 将此字符串转换为新的字符数组。
  • public static String valueOf(char[] data)
    返回char数组参数的字符串char形式。 字符数组的内容被复制; 字符数组的后续修改不会影响返回的字符串。
String str1 = "abd";
char[] c = str1.toCharArray();
for(char s : c) {
System.out.println(s);//a b d
}
  • public static String valueOf(char[] data)
    返回char数组参数的字符串char形式。 字符数组的内容被复制; 字符数组的后续修改不会影响返回的字符串。
  char[] c = {'a','b','c','d'};
     String str = null ;
     str = String.valueOf(c);
     System.out.println(str);//abcd

System.arraycopy() 和 Arrays.copyOf

  • Arrays.copyOf(原来的数组,新长度)方法返回的数组是新的数组对象,该拷贝不会影响原来的数组。
  • arraycopy(Object src,int srcPos, Object dest, int destPos,int length);
int[] a = new int[] { 1, 2, 3 };
int[] b = Arrays.copyOf(a, 5);
System.out.println(b.length);//5
for (int i : b) {
			System.out.println(i);}//1 2 3 0 0
int[] a = new int[] { 1, 2, 3 };
int[] b = new int[6] ;
System.arraycopy(a, 0, b, 0, 3);
for (int i : b) {
			System.out.println(i);
}//1 2 3 0 0 0

StringBuffer StringBuilder

  • StringBuffer 线程安全
  • StingBuilder 速度快,线程不安全
  • 方法
    • append
    • reverse
    • delete(int start, int end) 左包含右不包含
    • replace(int start, int end, String str)
StringBuffer sb = new StringBuffer("nishizhu");
		sb.append(" hahaha");
		sb.replace(0, 1, "wo");
		System.out.println(sb);

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值