StringAPI

package se1.day01;


public class StringDemo01 {
public static void main(String[] args) {
/**
* "字符串对象"是不能改变的.
*/
String s1 = "ABC";
String s2 = s1;
s1 = s1 + "DEF";
System.out.println(s1);
System.out.println(s2);
}


}

--------------------------------------------------------------------

package se1.day01;


public class StringDemo02 {

public static final String S = "ABC";


public static void main(String[] args) {
/**
* == 用于比较两个变量的"值"是否相等
*/
int a = 5;
int b = 5;

boolean l = a==b; //l
System.out.println(l);//true

/**
* String 字符量的缓存重用现象 
* 提高字符串的性能!!!节省内存耗用

* 1. 直接写出的量称为字面量:"ABC" 5 3.14
* "ABC" 字符串字面量
* 2. Java 为了性能,优化字符串"字面量"对象
*    将String对象引用缓存到常量池.
* 3. 使用相同字符串"字面量"时候, 替换为
*    相同的字符串对象引用
* 4. 字符串常量 和 字面量连接的结果 也作为
*     字面量优化
* 5. 其他通过new运算,或者字符串变量连接的
*    结果是新字符串对象,不参与常量池优化
*/
String s1 = "ABC";
String s2 = s1;
//s1==s2 返回true表示s1和s2中的地址值是
//相等的,也说明s1和s2引用了同一个对象
System.out.println(s1==s2);//true

String s3 = "ABC";
//s1==s3返回true表示s1和s3中的地址值是
//相等的,也说明s1和s3引用了同一个对象
System.out.println(s1==s3);//true

String s4 = "A";
String s5 = "BC";
String s6 = s4+s5;
//字符串变量连接结果不参与常量池优化
System.out.println(s6==s1);//false

//字符串常量和字符串字面量一同优化
System.out.println(S==s1);//true
//"字面量"的连接结果
String s7 = "A"+"B"+'C';//"ABC"
System.out.println(s7==s1);//true

//经典面试题目:
String s8 = new String("ABCD");
//如上程序运行期间创建了几个字符串对象:
//A.1个, B.2个  C.3个  D.4个


}


}






package se1.day01;


public class LengthDemo03 {


public static void main(String[] args) {
/**
* String 类型提供了length()方法
* 返回字符串中字符的个数

* 面试点: 数组长度是属性, 字符串长度是方法

* length: 长度
*/
String s1 = "Hello 世界!";
//           0123456 7 8
int l = s1.length();
System.out.println(l);//9
char c = s1.charAt(6);//按照位置获取字符
System.out.println(c);//世
/**
* getBytes()对字符串进行编码, 并且返回
* 编码以后的结果, 不同编码返回字节数量不同!
* getByte方法返回在字节长度不是内存中
* 实际占用的字节长度! 以后在编码课程专门讲
*/
byte[] bytes = s1.getBytes();
System.out.println(bytes.length);
}
}

-----------------------------------------------------------------------

package se1.day01;


import java.util.Scanner;


import com.sun.java_cup.internal.runtime.Symbol;


public class IndexOfDemo04 {
public static void main(String[] args) {
/**
* indexOf方法
* 在一个字符串中检索指定字符串出现的位置
* 如果找到了字符串,返回字符串的位置,否则
* 返回 -1

* 如果一个人的姓名第一个字符是"王"的话,则
* 这个人姓王

*/
Scanner in = new Scanner(System.in);
System.out.print("输入姓名:");
//            "宋哲"
String name = in.nextLine(); // "王宝强";
//             0 1 2
int n = name.indexOf("欧阳");//"欧阳"
System.out.println(n);
if(n==0){
System.out.println("这位仁兄姓欧阳"); 
}
}
}

--------------------------------------------------------------------------------

package se1.day01;


public class IndexOfDemo05 {


public static void main(String[] args) {
/**
* indexOf(字符, 位置)
* 从指定位置开始查找字符
*/
//跳过 "http://"  查找 / 的位置
String url="http://tedu.cn/index.html";
//          0123456789012345678901234
int n = url.indexOf("/", 7);
System.out.println(n); 
/**
* lastIndexOf() 从后向前查找指定字符串
* (字符)找到第一个就返回这个字符串的位置
* last 最后的
*/
n = url.lastIndexOf(".");
System.out.println(n); 
}
}

--------------------------------------------------------------------------

package se1.day01;


public class SubstringDemo06 {


public static void main(String[] args) {
/**
* 截取一个字符串的部分作为子字符串
*/
String url="http://tedu.cn/index.html";
//          0123456789012345678901234
String host = url.substring(7, 14);
System.out.println(host);//tedu.cn
//substring支持按照长度截取字符串
//在url中从7位置开始截取4个字符
String name=url.substring(7, 7+4);
System.out.println(name);//tedu

/**
* 业务案例
* 从URL中截取 主机(host)地址

* http://tmooc.cn/index.html
* http://tedu.cn/index.html
* http://canglaoshi.org/day01/index.html
*/
url="http://canglaoshi.org/index.html";
int i=url.indexOf("/",7);
host = url.substring(7, i);
System.out.println(host);

/**
*  substring(起始位置)
*/
String str="18岁给我一个姑娘";
//          012 3 4 5 67 8
String s = str.substring(7);
System.out.println(s); 
/**
* 从URL中截取访问的文件名
*/
i=url.lastIndexOf("/");
String file = url.substring(i+1);
System.out.println(file); 
}


}

------------------------------------------------------------------

package se1.day01;


public class TrimDemo07 {
public static void main(String[] args) {
/**
* trim 去除字符串两端的空白
* 空白: 包括 空格,Tab,回车,换行等
*/
String str = " \n \r  Tom \t \r \n ";
String s = str.trim();
System.out.println(s);//Tom
System.out.println(s == str);//false

str = "Tom";
s = str.trim();
System.out.println(s==str);//true

//经典题目: 
String s1 = "   ABC   ";
s1.trim();
System.out.println(s1);
//如上代码的输出结果是:
//A."ABC"      B."   ABC   " 
//C.编译错误        D.运行异常
//答案: B 
}
}

----------------------------------------------------------------------------------

package se1.day01;


public class EndsWithDemo08 {
public static void main(String[] args) {
/**
* 检查一个字符串是否已指定的字符串为结尾

*/
String file = "logo.png"; 
//检查文件是否为 png 图片文件
boolean b = file.endsWith(".png");
System.out.println(b);

//如果检查空串,总是返回true
b = file.endsWith("");
System.out.println(b);
//和字符串自己比较,返回true
b = file.equals("logo.png");
System.out.println(b); 



}
}



------------------------------------------------------------------------

package se1.day01;


public class StringBuilderDemo09 {


public static void main(String[] args) {
/**
* StringBuilder: 内容可变的字符串,简称
* 可变字符串, 与其相反 String 是内容不可
* 变的字符串, 简称不变字符串

* StringBuilder的优点是操作性能好
*/
StringBuilder buf=new StringBuilder();
//检查有效字符个数
System.out.println(buf.length());//0
//检查StringBuilder中的数组容量
System.out.println(buf.capacity());//16

buf.append('范');
//检查有效字符个数
System.out.println(buf.length());//1
//检查StringBuilder中的数组容量
System.out.println(buf.capacity());//16

buf.append("传奇");
buf.append("那一年, 在他18岁的时候");
//检查有效字符个数
System.out.println(buf.length());//1
//检查StringBuilder中的数组容量
System.out.println(buf.capacity());//16

buf.append("他说:我还年轻,给我一个姑娘");
//检查有效字符个数
System.out.println(buf.length());//1
//检查StringBuilder中的数组容量
System.out.println(buf.capacity());//16
System.out.println(buf);

//StringBuilder方法返回自身对象.
//适合连续"函数式编程"
buf.insert(3, "老湿")
  .delete(5, 5+3)
  .replace(1, 1+2, "**")
  .append("!")
  .append("!")
  .append("!");

System.out.println(buf);

}
}

----------------------------------------------------------

package se1.day01;


public class StringBuilderDemo10 {
public static void main(String[] args) {
/**
* StringBuilder的操作性能好于String

*/
String s1 = "A";
//s1 = s1+"A"; 
//s1 = new StringBuilder(s1)
//     .append("A").toString();
//s1 = s1+"A";
System.out.println(s1);
test1(50000);
test2(50000);
}
public static void test1(int n){
long t1=System.nanoTime();
String s1="";
for(int i=0; i<n; i++){
s1=s1+"A"; //new StringBuilder()
}
System.out.println(s1.length());
long t2=System.nanoTime();
System.out.println(t2-t1);
}

public static void test2(int n){
long t1=System.nanoTime();
StringBuilder buf=new StringBuilder();
for(int i=0; i<n; i++){
buf.append("A");
}
System.out.println(buf.length());
long t2 = System.nanoTime();
System.out.println(t2-t1); 
}
}

------------------------------------------------------------------------------

package se1.day01;


public class StringBuilderDemo11 {
public static void main(String[] args) {
/**
* 何时使用StringBuilder优化String连接
*/
//1.静态字符串连接, 不需要用StringBuilder!
//  静态字符串在编译阶段编译为一个对象!
String s1 = "A" + "bcd" + "DEF";
System.out.println(s1); 
String s2 = new StringBuilder("A")
.append("bcd").append("DEF")
.toString();
System.out.println(s2); 
//2. 写在一行上的字符串连接,不用StringBuilder
//   Java编译器会自动将一行上的字符串连接
//   优化为一个StringBuilder对象
String s3 = "ABC";
String s4 = "DEF";
String s5 = "EFG";

String ss = s3+s4+s5;
// ss = new StringBuilder(s3)
//       .append(s4)
//       .append(s5).toString();

//3. 多行多次操作字符串时候需要优化
//案例: 将一个整数数组连接为一个字符串
//  {1, 6, 8, 9, 10} 连接为 
//  "[1, 6, 8, 9, 10]"

//错误的写法: 运行期间创建了多个StringBuilder
int[] arr={1, 6, 8, 9, 10};
String str="["+arr[0];
for(int i=1; i<arr.length; i++){
str = str+", "+arr[i];
}
str+="]";
System.out.println(str); 

//正确的写法: 使用一个StringBuilder
StringBuilder buf=new StringBuilder("[");
buf.append(arr[0]);
for(int i=1; i<arr.length; i++){
buf.append(", ").append(arr[i]);
}
buf.append("]");
String str2 = buf.toString();
System.out.println(str2);
}


}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值