package String构造方法;
/*
* 关于String类中的构造方法
* 1、String s = "fsd";
* 2、String s = new String("fd");
* 3、String s = new String(char数组);
* 4、String s = new String(char数组,下标,长度);
* 5、String s = new String(byte数组);
* 6、String s = new String(char数组,下标,长度);
* */
public class Note01 {
public static void main(String[] args) {
//创建字符串最常用的一种方式
String s1 = "hello";
//这里只掌握常用的构造方法
byte[] bytes = {97,98,99};//a,b,c,的ASCII码
String s2 = new String(bytes);
System.out.println(s2);
/* 这里自动调用s2的toString()方法
分析:java中String类重写了toString()方法了
应该会输出字符串,输出结果:abc
*/
//下标地址1,长度2 ,数组bytes
//byte数组很长,只希望将byte中一部分转换成字符串,则调用这个方法
String s3 = new String(bytes,1,2);
System.out.println(s3);
char[] b = {'我','是','中','国','人'};
String s4 = new String(b);
System.out.println(s4);
String s5 = new String(b,1,4);
System.out.println(s5);
}
}
package String构造方法;
//一律掌握,以后会用到
public class Test01 {
public static void main(String[] args) {
//String中常用方法
//1.char charAt(int index);
char c = "中国人".charAt(1);
System.out.println(c);
//关于双引号. 调用方法的分析:“”是一个字符串对象,储存在字符串常量池中
//了解,下面方法
//int compareTo(String anotherString)
// 比较两个字符串的字典,负数是小,整数是大,0是相等,(前后比较)
// 字符串第一个字符,和第二个字符串第一个字符比较,如果第一个字符相等就向下比较
System.out.println("Ac".compareTo("Abc"));
//contains(CharSequence s) 掌握
//如果并且只有当此字符串包含指定的字符序列的字符串值,则返回真值。
System.out.println("\n-------------");
System.out.println("hello".contains("lo"));
//endsWith(String suffix) 掌握
//测试如果这个字符串以指定的后缀结束。
System.out.println("------------");
System.out.println("hello".endsWith("o"));
System.out.println("hello".endsWith("sd"));
//equals(Object anObject)
//将此字符串与指定的对象比较。掌握
//equals方法有没有调用compareTo方法?没有,老版本有调
System.out.println("\n-------------");
System.out.println("abc".equals("abc"));
//equalsIgnoreCase(String anotherString)
//判断两个字符串是否相等,忽略大小写
//equals只能看出相等不相等,compare不仅能看出是否相等,还能看出大小
System.out.println("ABC".equalsIgnoreCase("abc"));
//getBytes() 将字符串对象转换成字节数组 掌握
System.out.println("\n-------------");
byte[] saa = "abcdef".getBytes();
for (int i = 0; i < saa.length; i++) {
System.out.print(" " + saa[i]);
}
//indexOf(String str) 掌握
//返回在这个字符串指定的子字符串中第一个出现的索引。
System.out.println("\n-------------");
System.out.println("sdgsagisdfjgweisdf".indexOf("fj"));
//lastIndexOf(int ch)
//返回在指定字符的最后一个发生的字符串内的索引。
System.out.println("\n-------------");
System.out.println("dasfaagdadagdadadf".lastIndexOf("ad"));
//isEmpty() 掌握
//判断字符串是否为空,不能为null,会出现空指针异常,完全没必要写这个方法,直接.length()更好
System.out.println("\n-------------");
System.out.println("iijn".isEmpty());
System.out.println("efsadf".length());
//replace(CharSequence target, CharSequence replacement)掌握
//每个子串替换该字符串指定的文本替换序列靶序列匹配的文字。
System.out.println("\n-------------");
String aa = "www.baidu.com".replace("baidu","360");
System.out.println(aa);
//String[] split(String regex)
//拆分字符串 掌握*****
System.out.println("\n-------------");
String[] vv ="1999-112-12-23".split("-");
for (int i = 0; i < vv.length; i++) {
System.out.print(" " + vv[i]);
}
//boolean startsWith(String prefix) 掌握
//判断某个字符串是否以某个子字符串开始
System.out.println("\n-------------");
System.out.println("www.baidu.com".startsWith("baidu"));
System.out.println("www.baidu.com".startsWith("www."));
//String substring(int beginIndex)
//截取字符串
System.out.println("\n-------------");
System.out.println("www.baidu.com".substring(4));//baidu.com
//String substring(int beginIndex, int endIndex)
//指定位置截取,左边包含,右边不包含 掌握
System.out.println("\n-------------");
System.out.println("www.baidu.com".substring(4,9));
//char[] toCharArray()
//将字符串转换为char数组
System.out.println("\n-------------");
char[] kkkk = "我是中国人".toCharArray();
for (int i = 0; i <kkkk.length ; i++) {
System.out.print(kkkk[i] + " ");
}
//String toLowerCase()
//转换为小写
System.out.println("\n-------------");
System.out.println("ESFISGsdvfkHJ".toLowerCase());
//String toUpperCase()
//转换为大写
System.out.println("\n-------------");
System.out.println("sgfsdgfshg".toUpperCase());
//String trim()
//去除字符串空白,前面和后面的空白,中间不能去
System.out.println("\n-------------");
System.out.println(" dggs dsgi sdg sgji ".trim());
//String中只有一个方法是静态的,不需要new对象,类名去调用,String.valueof("")
//方法valueof,作用:将“非字符串”转换成“字符串”
System.out.println("\n-------------");
System.out.println(String.valueOf(true));
System.out.println(String.valueOf(100));
//这个静态的valueof()方法,参数是一个对象的时候,会自动调用该对象的toString方法吗?
String jj = String.valueOf(new Customer());
System.out.println(jj);//没有重写前是对象内存地址,意思就是将内存地址转换为字符串
//重写后,就是将重写的toString方法的返回值,转换为字符串
//有点绕
//看下println()方法的源代码
System.out.println(123);
System.out.println(3.12);
System.out.println(true);
System.out.println(new Customer());
//本质上就是将里面的东西,转换为字符串再输出,调valueof转换的
/*
*
* 重点:问为什么System.out.println();可以输出与任意类型?
* 答:因为
*/
}
}
class Customer{
@Override
public String toString() {
return "哈哈哈哈哈";
}
}