/**
*1、Java字符串String详解
* String实例化的两种方式
*
*/
public class StringDemo1 {
public static void main(String[] args) {
String str1 = "hello1";
System.out.println(str1);
//这种方式会在栈中开辟两个空间,第一个空间是没用的,等待回收站回收
//所以我们通常用第一种方式得到string的对象
String str2 = new String("hello2");
System.out.println(str2);
}
}
package com.zhh.java.string;
/**
* 1、Java字符串String详解
*字符串比较
*
*/
public class StringDemo2 {
public static void main(String[] args) {
String str1 = "hello";
String str2 = new String("hello");
System.out.println(str1 == str2);
System.out.println(str1.equals(str2));
//比较的是位置(在栈中的空间的位置)
//equals比较的是内容
}
}
package com.zhh.java.string;
/**
* 1、Java字符串String详解
*String字符串不可更改
*/
public class StringDemo3 {
public static void main(String[] args) {
String str1 = "hello";
String str2 = str1 + "world";
System.out.println(str2);
//表面上是改变了,内容打印出来是helloworld
//实际上在堆中先开辟了一个存放hello的内存,栈str1指向他
//堆中在开辟了一个内存存放world
//最后又开辟了一个内存存放helloworld,栈str2指向他
//以前存放hello的内存的空间,仍然放的是hello,没有改变
}
}
package com.zhh.java.string;
/**
* 2、Java String字符串常用方法
*
*/
public class StringDemo4 {
public static void main(String[] args) {
// method1();
// method2();
// method3();
// method4();
// method5();
// method6();
// method7();
// method8();
// method9();
// method10();
method11();
}
/**
* 字符串的长度
*/
public static void method1() {
String s = "hfksdhlkfhdaslkj";
System.out.println(s.length());
}
/**
* 字符串转化成数组
*/
public static void method2() {
String str = "hfksdhlkfhdaslkj";
char[] data = str.toCharArray();
for (int i = 0; i < data.length; i++) {
System.out.print(data[i] + " ");
}
}
/**
* 得到当前字符串中的某一个字符
*/
public static void method3() {
String str = "hfksdhlkfhdaslkj";
System.out.println(str.charAt(0));
}
/**
* 转化成字节数组
*/
public static void method4() {
String str = "hfksdhlkfhdaslkj";
byte[] b = str.getBytes();
for (int i = 0; i < b.length; i++) {
System.out.println(new String(b));
}
}
/**
* 检测有没有某个字符
* 这个是非常有用的
*/
public static void method5() {
String str = "hfk@";
//如果有返回的是字符的位置,没有返回-1
System.out.println(str.indexOf("@"));
System.out.println(str.indexOf("Q"));
}
/**
* 去掉字符串的前后空格
*/
public static void method6() {
String str = " hfk";
System.out.println(str.trim());
System.out.println(str);
}
/**
* 从字符串中取出字字符串substring(beginIndex)
*/
public static void method7() {
String str = "abcde";
System.out.println(str.substring(2));
}
/**
* 大小写转换
*/
public static void method8() {
String str = "abcde";
System.out.println(str.toLowerCase());//转换成小写
System.out.println(str.toUpperCase());//转换成大写
}
/**
* 判断字符串开头和结尾字符
*/
public static void method9() {
String str = "abcde";
System.out.println(str.startsWith("a"));//开头是a,返回true,不是a,返回false
System.out.println(str.endsWith("e"));//结尾是e,返回true,不是e,返回false
}
/**|
* 替换string中的一个字符
*/
public static void method10() {
String str = "abcde";
System.out.println(str.replace("a", "1"));
}
/**
* 拆分字符串
*
*/
public static void method11() {
String str = "abcde";
String[] s = str.split("b");//根据字符串中的“b”拆分字符串
for (int i = 0; i < s.length; i++) {
System.out.println(s[i]);
}
}
}
package com.zhh.java.string;
/**
* 3、Java StringBuffer方法
*
*/
public class StringBufferDemo1 {
public static void main(String[] args) {
method6();
}
/**
*
* 追加内容
*/
public static void method1() {
StringBuffer stringBuffer = new StringBuffer();
System.out.println(stringBuffer.append("中国人").toString());
}
/**
* String不能被更该
*/
public static void method2() {
String str = "hello";
System.out.println(str);
tellStr(str);
System.out.println(str);
}
private static void tellStr(String str) {
str = "helloaaaa";
}
/**
* StringBuffer能被更该
*/
public static void method3() {
StringBuffer str = new StringBuffer("hello");
System.out.println(str.toString());
tellSb(str);
System.out.println(str.toString());
}
private static void tellSb(StringBuffer str) {
str.append("aaaa");
}
/**
* 插入
*/
public static void method4() {
StringBuffer sb = new StringBuffer("hello");
System.out.println(sb.insert(0, "123"));//0代表插入的位置,123代表插入的类容
}
/**
* 替换
*/
public static void method5() {
StringBuffer sb = new StringBuffer("hello");
//把第一个到第三个位置替换为222
System.out.println(sb.replace(1, 3, "222"));
}
/**
* 检测有没有某个字符
* 这个是非常有用的
* 和String中的完全一样
*/
public static void method6() {
StringBuffer str = new StringBuffer("hfk@");
//如果有返回的是字符的位置,没有返回-1
System.out.println(str.indexOf("@"));
System.out.println(str.indexOf("Q"));
}
}
package com.zhh.java.string;
/**
* 3、Java StringBuffer方法
*
*/
public class StringBufferDemo2 {
/**
* StringBuffer追加
* 如果用String的话就会不断地创建空间,效率很低
*
*/
public static void main(String[] args) {
method2();
}
/**
* StringBuffer追加
* 如果用String的话就会不断地创建空间,效率很低
*
*/
public static void method1() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("中国人");
for (int i = 0; i < 10; i++) {
stringBuffer.append(i);
}
System.out.println(stringBuffer.toString());
}
/**
* StringBuffer追加
* 如果用String的话就会不断地创建空间,效率很低
*
*/
public static void method2() {
String string = "中国人";
for (int i = 0; i < 10; i++) {
string = string + i;
}
System.out.println(string);
}
}
package com.zhh.java.string;
/**
* 4、Java StringBuider用法
* 用在字符串缓冲区被单个线程使用时,建议优先考虑StringBuilder,速度比StringBuffer要快
* 涉及到线程安全方面的建议使用StringBuffer
* 他的使用方法和StringBuffer一样这里就不在说明了
*
*
*/
public class StringBuilderDemo1 {
public static void main(String[] args) {
StringBuilder builder = new StringBuilder();
}
}
源码下载:
http://download.csdn.net/detail/zhaihaohao1/8741715
视频下载:
http://c38.yunpan.360.cn/my/index/#%2F%E7%A8%8B%E5%BA%8F%E5%BC%80%E5%8F%91%2Fjava%2F