String
- 系统提供的类String List Ma
- 1.不可变的字符串 一旦创建 内容不能改变
- StringBuffer 可变字符串
- StringBuilder 可变字符串
// == 比较两个对象是否相同:比较的是地址
// equals 比较的是内容是否相同
String str1 = "abc";
String str2 = "abc";
System.out.println(str1==str2);
System.out.println(str1.equals(str2));
//使用构造方法
//null 和""的区别
String str3=new String();
System.out.println();
System.out.println("");
//使用字节数组 创建一个字符串
byte [] name ={'a','b','c'};
String str4=new String(name);
System.out.println(str4);
byte [] name1 ={84,85,86};
String str5=new String(name1);
System.out.println(str5);
//使用字节数组的一部分 创建一个字符串
String str6=new String(name,0,2);
System.out.println(str6);
char [] hellow ={'你','好','啊'};
String str7=new String(hellow);
System.out.println(str7);
//字符串有哪些方法
//获取字符串中的一个字符
//使用 charAt
char c=str7.charAt(0);
System.out.println(c);
//两个字符串的比较 compareTo
//可以知道大小关系 0:相同 >0:大于 <0:小于
int result =str4.compareTo(str5);
System.out.println(result);
//两个字符串的连接
//使用 concat
String Str8=str5.concat(str7);
System.out.println(Str8);
//判断一个字符串hi否包含另一个字符串
//contains
boolean r="hollow".contains("ow");
System.out.println(r);
//判断是否以某个字符串结尾
//使用 endsWith
String u="http:www.jianshu.com";
if (u.endsWith(".com"))
{
System.out.println("网站");
}
//判断是否以某个字符串开头
//使用 startsWith
if(u.startsWith("http"))
{
System.out.println("http协议");
}
//两个字符串进行比较
//使用equals
if ("abc".equals("ABC"))
{
System.out.println("相同");
}
else
{
System.out.println("不相同");
}
//判断一个子字符串在另一个字符串里面的位置
//使用indexOf (汉译:查找字符串第一次出现的地方) 不存在返回值为-1
String i1="I am a good people";
int index =i1.indexOf("good people");
System.out.println(index);
//获取字符串的长度
//使用 length
//获取子字符串的长度
// 使用 substring 从index(汉译:指针) 到结尾
String s=i1.substring(5);
System.out.println(s);
String s1=i1.substring(4,8);
System.out.println(s1);
//将字符串转化为字符数组
//toCharArray
String d="abcdefg";
char e[]=d.toCharArray();
for(int i=0;i<d.length();i++)
{
System.out.println(e[i]);
}
//将所有的字符转化为小写/大写
// toLowerCase/toUpperCase
d.toUpperCase();
System.out.println(d.toUpperCase());
//将字符串前面和后面的空格字符删除
//使用trim
String f=" a b c d e f g ";
f.trim();
System.out.println(f.trim());
}
StringBuilder
// 可变字符串
// StringBuffer 线程安全 效率不高
// StringBuilder 线程不安全的 效率更高
// insert append delete replace
// 创建的同时先准备好6个字符的空间
StringBuilder sb = new StringBuilder(6);
// append 在末尾追加
sb.append("I");
sb.append(" Love");
sb.append(" Android");
System.out.println(sb);
// insert 插入数据
sb.insert(2, "also ");
System.out.println(sb);
// replace 替换
// start end string
int start = sb.indexOf("Android");
int end = start + "Android".length();
sb.replace(start,end,"you!!!");
System.out.println(sb);
// reverse 反转
sb.reverse();
System.out.println(sb);
}
}
接口实现回调 代理设计模式
1.主函数
public class MyClass {
public static void main(String[]args){
Read read=new Read("test");
read.gotoSetting();
Chat chat =new Chat("test");
chat.gotosetting();
}
}
2.read 阅读界面
public class Read implements Setting.abc {
private String test;
private String color;//默认的字体颜色
private int size;//默认的字体大小
public Read(String test)
{
this.test=test;
}
public void gotoSetting()
{
//创建设置页面的对象(this)
Setting setting =new Setting(this);
//推送到设置页面
setting.startSetting();
}
@Override
public void change(String color, int size) {
System.out.println("改变前的颜色"+this.color+"改变前的大小"+this.size);
this.color=color;
this.size=size;
System.out.println("改变后的颜色"+this.color+"改变后的大小"+this.size);
}
}
chat 聊天界面
public class Chat implements Setting.abc {
String color;
String test;
int size;
public Chat(String test)
{
this.test=test;
}
public void gotosetting()
{
Setting setting=new Setting(this);
setting.startSetting();
}
@Override
public void change(String color, int size) {
System.out.println("颜色"+this.color+"大小"+this.size);
this.color=color;
this.size=size;
System.out.println("颜色"+this.color+"大小"+this.size);
}
}
setting设置界面
public class Setting {
abc object;
private int size;//默认的字体大小
private String color;
public Setting(abc object)
{
this.object=object;
}
public void startSetting() {
System.out.println("开始设置");
System.out.println("**************");
System.out.println("设置完毕");
object.change("红色",20);
}
/**
* 接口 抽象类 普通类
* 是否需要添加成员变量 需要:抽象类 普通类
* 不需要:接口
*添加的方法是否必须实现 是:抽象类 接口
* 否:普通类
* 是提供模板 还是通信方式 模板:抽象类
* 通信方式:接口
* * 接口 interface 定义一套(两者之间相互转换)方法 用于对象之间的通信
*/
public interface abc
{
void change(String color,int size);
}
}
感悟:今天讲的东西不多,准确的来说不是很难,就一个小程序和String讲解,很容易理解,期待今后的学习像这样轻松。