1:String类的概述和使用
其实它可以和字符数组进行相互转换。
A:public String() 初始化一个新创建的String对象,使其表示一个空字符序列. B:public String(byte[] bytes) 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的String. C:public String(byte[] bytes,int offset,int length) 通过使用平台的默认字符集解码指定的 byte 子数组,构造一个新的String. D:public String(char[] value) 分配一个新的String,使其表示字符数组参数中当前包含的字符序列。 E:public String(char[] value,int offset,int count) 分配一个新的String,它包含取自字符数组参数一个子数组的字符。 F:public String(String original) 初始化一个新创建的String对象,使其表示一个与参数相同的字符序列;换句话说,新创建的字符串是该参数字符串的副本。
G:String s = 'hello';
A:字符串一旦被赋值,就不能改变。 注意:这里指的是字符串的内容不能改变,而不是引用不能改变。 B:字面值作为字符串对象和通过构造方法创建对象的不同 String s = new String('hello');和String s = 'hello'的区别?
A:==和equals() String s1 = new String('hello'); String s2 = new String('hello'); System.out.println(s1 == s2);// false System.out.println(s1.equals(s2));// true String s3 = new String('hello'); String s4 = 'hello'; System.out.println(s3 == s4);//false System.out.println(s3.equals(s4));//true String s5 = 'hello'; String s6 = 'hello'; System.out.println(s5 == s6);// true System.out.println(s5.equals(s6));// true B:字符串的拼接 String s1 = 'hello'; String s2 = 'world'; String s3 = 'helloworld'; System.out.println(s3 == s1 + s2);// false System.out.println(s3.equals((s1 + s2)));// true System.out.println(s3 == 'hello' + 'world');//true System.out.println(s3.equals('hello' + 'world'));// true
A:判断功能 boolean equals(Object obj) boolean equalsIgnoreCase(String str) boolean contains(String str) boolean startsWith(String str) boolean endsWith(String str) boolean isEmpty() ++++++++++++++++++++++++++++++++++++++++++++++++++ B:获取功能 int length() char charAt(int index) int indexOf(int ch) int indexOf(String str) int indexOf(int ch,int fromIndex) int indexOf(String str,int fromIndex) String substring(int start) String substring(int start,int end) ++++++++++++++++++++++++++++++++++++++++++++++++++ C:转换功能 byte[] getBytes() char[] toCharArray() static String valueOf(char[] chs) static String valueOf(int i) String toLowerCase() String toUpperCase() String concat(String str) ++++++++++++++++++++++++++++++++++++++++++++++++++ D:其他功能 a:替换功能 String replace(char old,char new) String replace(String old,String new) b:去空格功能 String trim() c:按字典比较功能 int compareTo(String str) int compareToIgnoreCase(String str) 综合案例: ++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++ 2:Scanner的使用 (1)在JDK5以后出现的用于键盘录入数据的类。 (2)构造方法: A:讲解了System.in这个东西。 它其实是标准的输入流,对应于键盘录入 B:构造方法 InputStream is = System.in; Scanner(InputStream is) C:常用的格式 Scanner sc = new Scanner(System.in); (3)基本方法格式: A:hasNextXxx() 判断是否是某种类型的 B:nextXxx()返回某种类型的元素 (4)要掌握的两个方法 A:public int nextInt() B:public String nextLine() (5)需要注意的小问题 A:同一个Scanner对象,先获取数值,再获取字符串会出现一个小问题。 B:解决方案: a:重新定义一个Scanner对象 b:把所有的数据都用字符串获取,然后再进行相应的转换 这里提供的只是曾经学习整理的笔记,勿喷,谢谢,仅供java爱好者参考! 每日更新,请大家多多关注.谢谢!!! |
|