美文网首页
Java String类

Java String类

作者: 小石头呢 | 来源:发表于2019-08-15 00:36 被阅读0次

一.创建字符串

1.直接创建

String str = "123";
str = "1234";
  • 编译器会使用该值创建一个 String 对象

  • String类是不可以改变的,所以一旦创建值就不能改变,但是可以改变指向关系,指向一个新的字符串。

String str1 = "asd";
String str2 = "asd";

//比较两个对象是否相同,比较地址
System.out.println(str1 == str2);
//true

//比较两个内容是否相同
System.out.println(str1.equals(str2));
//true
  • 因为String类是不可以改变的,所以它在内存中是被共享的。所有内容相同的字符串的内存位置相同。

  • ==用来判断两个对象是否是同一个对象,所以比较的是两个对象的地址。

  • equals比较的是两个字符串对象的内容是否相等。

2.构造方法

String 类有 11 种构造方法,这些方法提供不同的参数来初始化字符串。

  • 字符串常量初始化
String temp = new String("nihao");
  • 字节数组初始化
//结果相同都是abc
byte[] name = {'a','b','c'};
String str = new String(name);

byte[] name = {97,98,99};
String str = new String(name);
  • 使用字节数组的一部分初始化
//第一个参数的索引处到第二个参数的索引处
//不包括第二个参数的索引
byte[] name = {'a','b','c'};
String str = new String(name,1,1);
  • 使用字符数组初始化
char[] name = {'你','好','啊'};
String str = new String(name);

byte和char的区别:byte占据一个字节,char占据两个字节,byte更省内存。

二.创建格式化字符串

  • 输出格式化数字可以使用 printf() 和 format() 方法。

  • String 类的静态方法 format() 能用来创建格式化字符串

String fs;
fs = String.format("浮点型变量的值为 " +
                   "%f, 整型变量的值为 " +
                   " %d, 字符串变量的值为 " +
                   " %s", floatVar, intVar, stringVar);

三.String方法

  • 返回指定索引处的 char 值。
public char charAt(int index)
char[] name = {'你','好','啊'};
String str = new String(name);

char c = str.charAt(0);
//你
  • 按字典顺序比较两个字符串。
//按照字典比较其实说的是比对ASCII码的值
//compareTo(String)方法中其实是从头开始
//一个一个字符的比对原字符串和参数字符串中的字符
//如果相同就下一个,直到字符出现不同(包括某一个字符串结束了)
//就计算这两个不同字符的ASCII码的差,作为返回值。
//如果参数字符串等于此字符串,则返回值 0;
//如果此字符串小于字符串参数,则返回一个小于 0 的值;
//如果此字符串大于字符串参数,则返回一个大于 0 的值。
int compareTo(String anotherString)

//不考虑大小写
int compareToIgnoreCase(String str)
  • 将指定字符串连接到此字符串的结尾,返回一个新的字符串。
public String concat(String s)

char[] name1 = {'你','好','啊'};
String str1 = new String(name1);

byte[] name2 = {'a','b','c'};
String str3 = new String(name2);

String str = str1.concat(str2);
//你好啊abc
  • 用于将此字符串与指定的 StringBuffer 比较,当且仅当字符串与指定的StringBuffer有相同顺序的字符时候返回真。
public boolean contentEquals(StringBuffer sb)

String str1 = "String"; 
StringBuffer str2 = new StringBuffer( "String");
boolean  result = str1.contentEquals( str2 );
//true
  • 返回指定数组中表示该字符序列的字符串。
public static String copyValueOf(char[] data)

//offset -- 子数组的初始偏移量,count -- 子数组的长度
//或者说从参数一的索引处到参数二的索引处,但是不包括参数二处的索引
public static String copyValueOf(char[] data, int offset, int count)
  • 判断该字符串是否以指定的字符串为后缀/前缀。
public boolean endsWith(String suffix)

public boolean startsWith(String prefix)

//toffset -- 字符串中开始查找的位置
public boolean startsWith(String prefix, int toffset)

String url = "http://www.baidu.com";
if (url.endsWith(".com")){
    System.out.println("网址");
}

if (url.startsWith("http")){
    System.out.println("http协议");
}

if (url.startsWith("www",7)){
    System.out.println("万维网");
}
  • 判断一个字符串是否包含另外一个字符串。
public boolean contains(String var)

boolean source = "hello".contains("hel");
//true
  • 将字符串与指定的对象比较。
public boolean equals(Object anObject)

if ("abc".equals("bcd")){
    System.out.println("相同");
}else {
    System.out.println("不相同");
}
//不相同

//不考虑大小写
public boolean equalsIgnoreCase(String anotherString)
  • 将字符串转化为byte数组。
//使用平台的默认字符集将字符串编码为 byte 序列
//并将结果存储到一个新的 byte 数组中
public byte[] getBytes()

//使用指定的字符集将字符串编码为 byte 序列
//并将结果存储到一个新的 byte 数组中
public byte[] getBytes(String charsetName) 
                                 throws UnsupportedEncodingException
  • 将字符从字符串复制到目标字符数组。
//srcBegin -- 字符串中要复制的第一个字符的索引。
//srcEnd -- 字符串中要复制的最后一个字符之后的索引。
//dst -- 目标数组。
//dstBegin -- 目标数组中的起始偏移量。
public void getChars(int srcBegin, int srcEnd, char[] dst,  int dstBegin)

public class Test {
    public static void main(String args[]) {
        String Str1 = new String("www.runoob.com");
        char[] Str2 = new char[6];

        try {
            Str1.getChars(4, 10, Str2, 0);
            System.out.print("拷贝的字符串为:" );
            System.out.println(Str2 );
        } catch( Exception ex) {
            System.out.println("触发异常...");
        }
    }
}
//拷贝的字符串为:runoob
  • 返回字符串的哈希码。
public int hashCode()
  • 返回指定内容在字符串中第一次出现的索引,如果没有,则返回 -1。
//返回指定字符在字符串中第一次出现处的索引
public int indexOf(int ch)

//返回从 fromIndex 索引处开始查找指定字符在字符串中第一次出现处的索引
public int indexOf(int ch, int fromIndex)

//返回指定字符串在字符串中第一次出现处的索引
public int indexOf(String str)

//返回从 fromIndex 索引处开始查找指定字符串在字符串中第一次出现处的索引
public int indexOf(String str, int fromIndex)


public class Main {
    public static void main(String args[]) {

        String string = "aaa456ac";  

        //查找指定字符是在字符串中的下标。在则返回所在字符串下标;不在则返回-1.  
        System.out.println(string.indexOf("b")); // indexOf(String str); 返回结果:-1,"b"不存在  
 
        // 从第四个字符位置开始往后继续查找,包含当前位置  
        System.out.println(string.indexOf("a",3));//indexOf(String str, int fromIndex); 返回结果:6  
 
        //(与之前的差别:上面的参数是 String 类型,下面的参数是 int 类型)参考数据:a-97,b-98,c-99  
 
        // 从头开始查找是否存在指定的字符  
        System.out.println(string.indexOf(99));//indexOf(int ch);返回结果:7  
        System.out.println(string.indexOf('c'));//indexOf(int ch);返回结果:7  
 
        //从fromIndex查找ch,这个是字符型变量,不是字符串。字符a对应的数字就是97。  
        System.out.println(string.indexOf(97,3));//indexOf(int ch, int fromIndex); 返回结果:6  
        System.out.println(string.indexOf('a',3));//indexOf(int ch, int fromIndex); 返回结果:6  
    }
}
  • 返回指定内容在字符串中最后一次出现的索引,如果没有,则返回 -1。
//返回指定字符在字符串中最后一次出现处的索引
public int lastIndexOf(int ch)

//返回从 fromIndex 索引处开始查找指定字符在字符串中最后一次出现处的索引
public int lastIndexOf(int ch, int fromIndex)

//返回指定字符串在字符串中最后一次出现处的索引
public int lastIndexOf(String str)

//返回从 fromIndex 索引处开始查找指定字符串在字符串中最后一次出现处的索引
public int lastIndexOf(String str, int fromIndex)
  • 返回字符串的长度。
public int length()
  • 返回字符串的子字符串。
public String substring(int beginIndex)

//beginIndex -- 起始索引(包括), 索引从 0 开始
//endIndex -- 结束索引(不包括)
public String substring(int beginIndex, int endIndex)
  • 将此字符串转化为一个新的字符数组。
public char[] toCharArray()
  • 使用默认语言环境的规则将此 String 中的所有字符都转换为小写/大写。
//转换为小写
public String toLowerCase()

//转换为大写
public String toUpperCase()
  • 用于删除字符串的头尾空白符。
public String trim()
  • 返回给定类型参数的字符串表示形式。
static String valueOf(基本类型/对象类型  value) 

public class Test {
    public static void main(String args[]) {
        double d = 1100.00;
        boolean b = true;
        long l = 1234567890;
        char[] arr = {'r', 'u', 'n', 'o', 'o', 'b' };

        System.out.println("返回值 : " + String.valueOf(d) );
        System.out.println("返回值 : " + String.valueOf(b) );
        System.out.println("返回值 : " + String.valueOf(l) );
        System.out.println("返回值 : " + String.valueOf(arr) );
    }
}

//返回值 : 1100.0
//返回值 : true
//返回值 : 1234567890
//返回值 : runoob

相关文章

网友评论

      本文标题:Java String类

      本文链接:https://www.haomeiwen.com/subject/shyejctx.html