Object
1.概述
是类层次结构的根类。每个类都使用 Object
作为超类。
2.创建对象
Object() —无参构造
3.常用方法
String | toString() 返回该对象的字符串表示。 |
---|---|
boolean | equals(Object obj) 指示其他某个对象是否与此对象“相等”。 |
---|---|
int | hashCode() 返回该对象的哈希码值。 |
---|---|
String
1.概述
所有被一对双引号引起来的数据都是String类型
String
类代表字符串。字符串是常量;它们的值在创建之后不能更改。
String本质上是维护一个char[]来存储多个字符数据
2.源码
public final class String
private final char value[];常量,值不能被修改
3.创建对象
`**String**(char[] value)`
4.常用方法
String s=“helloworld”; System.out.println();
char | **charAt**(int index) 返回指定索引处的 char 值。 |
---|---|
System.out.println(s.charAt(2)); —‘l’ | |
String | **concat**(String str) 将指定字符串连接到此字符串的结尾。 |
System.out.println(s.concat(“123”));—“hello123” | |
boolean | **contains**(CharSequence s) 当且仅当此字符串包含指定的 char 值序列时,返回 true。 |
System.out.println(s.contains(“xyz”));—false | |
boolean | **endsWith**(String suffix) 测试此字符串是否以指定的后缀结束。 |
System.out.println(s.endWith(“o”));—true | |
boolean | **equals**(Object anObject) 将此字符串与指定的对象比较。 |
System.out.println(s.equals(“llo”));//false | |
int | **hashCode**() 返回此字符串的哈希码。 |
System.out.println(s.hashCode());//-1524582912 | |
int | **indexOf**(String str) 返回指定子字符串在此字符串中第一次出现处的索引。 |
System.out.println(s.indexOf(“wor”));//5 | |
boolean | **isEmpty**() 当且仅当 length() 为 0 时返回 true 。 |
System.out.println(s.isEmpty());//false | |
int | **lastIndexOf**(String str) 返回指定子字符串在此字符串中最右边出现处的索引。 |
System.out.println(s.lastIndexOf(“o”));//6 | |
int | **length**() 返回此字符串的长度。 |
System.out.println(s.length());//10 | |
String | **replace**(char oldChar, char newChar) 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。 |
System.out.println(s.replace(‘l’, ‘c’));//heccoworcd | |
boolean | **startsWith**(String prefix, int toffset) 测试此字符串从指定索引开始的子字符串是否以指定前缀开始。 |
System.out.println(s.startsWith(“e”));//false | |
String | **substring**(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串。 |
System.out.println(s.substring(1));//elloworld | |
String | **substring**(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串。含头不含尾 |
System.out.println(s.substring(1, 3));//el | |
String | **toLowerCase**() 使用默认语言环境的规则将此 String 中的所有字符都转换为小写。 |
System.out.println(s.toLowerCase());//helloworld | |
String | **toUpperCase**() 使用默认语言环境的规则将此 String 中的所有字符都转换为大写。 |
System.out.println(s.toUpperCase());//HELLOWORLD | |
String | **trim**() 返回字符串的副本,忽略前导空白和尾部空白。 |
System.out.println(s.trim());//helloworld //去除前面和后面的多余空格 | |
static String | **valueOf**(char c) 返回 char 参数的字符串表示形式。 |
System.out.println(String.valueOf(‘l’));//e 把各种类型转成String类型 | |
byte[] | **getBytes**() 使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。 |
byte[] a = s.getBytes(); System.out.println(Arrays.toString(a));//[104, 101, 108, 108, 111, 119, 111, 114, 108, 100] | |
char[] | **toCharArray**() 将此字符串转换为一个新的字符数组。 |
char[] b = s.toCharArray(); System.out.println(Arrays.toString(b));//[h, e, l, l, o, w, o, r, l, d] | |
String[] | **split**(String regex) 根据给定正则表达式的匹配拆分此字符串。 |
String[] c = s.split(“o”); System.out.println(Arrays.toString©);//[hell, w, rld] |
5.练习题
接受用户输入的字符串,并打印每个字符
String sc=new String(System.in).nextLine();
for(int i=0;i<sc.length();i++){
char x = sc.charAt(i);
System.out.println(x);
}