String类中的其他功能:
替换功能:
* public String replace(char oldChar,char newChar):将字符串中某一个字符用新的字符替换
* public String replace(String oldStr,String newStr):将字符串中某一个子字符串用新 的字符 串去替代
* public String trim():去除字符串两端空格:
* public int compareTo(String anotherString) 是Comparable接口中的方法(该接口可以实现一 个自然排序)
* Comparator接口可以比较器排序
package string;
public class StringDemo1 {
public static void main(String[] args){
//定义字符串
String str="helloword";
System.out.println(str);
//将原字符串中的字符“l”用“k”替换,
String str1=str.replace("l", "k");
System.out.println(str1);
//将原字符串中的字符串“hello”用“hi——”替换
String str2=str.replace("hello", "hi——");
System.out.println(str2);
System.out.println("----------------");
String str3=" helloword ";
System.out.println(str3);
//返回字符串的副本,忽略前导空白和尾部空白
String str4=str3.trim();
System.out.println(str4);
System.out.println("----------------");
String str5 ="helloword" ;
String str6="abc";
System.out.println("compareTo():"+str5.compareTo(str));
System.out.println("compareTo():"+str6.compareTo(str));
}
}* int indexOf(String str):返回指定字符串在此字符串中的一次出现的索引
* int indexOf(Stirng str,int fromIndex):返回次字符串中第一次出现指定字符串出的索引,从 指定索引开始
* String substring(int start):从指定位置开始截字符串,默认到最末尾
* String substring(int start,int end):从指定位置开始截取到指定位置结束
package string;
import java.util.Scanner;
public class StringDemo5 {
public static void main(String[] args){
String str="java";
Scanner sc=new Scanner(System.in);
System.out.println("请你输入一个字符串:");
String str1=sc.nextLine();
System.out.println("你输入的字符串是:"+str1);
System.out.println("----------------");
//从头开始返回java第一次出现的首地址
System.out.println("str1.indexOf(str):"+str1.indexOf(str));//ugfuyjavaojgojjavagfdug
//从指定索引开始
System.out.println("str1.indexOf(str,7 ):"+str1.indexOf(str,7 ));
System.out.println("-------------------");
//从指定位置截取字符串,默认到末尾结束
System.out.println("str1.substring(3):"+str1.substring(3));
//从指定位置截取字符串,到指定位置结束
System.out.println("str1.substring(3,7):"+str1.substring(3,7));
}
}* StringBuffer类
线程安全的可变字符序列,类似于String的字符串缓冲区
*开发中线程不安全的话,可能会出现死锁的现象!线程安全和执行效率是相对的
* 面试题
*StringBuffer与String的区别?
*StringBuffer会构造一个字符串缓冲区,从内存角度考虑使用StringBuffer比较多
String是一个普通的字符串类,从内存角度耗费空间!
在单线程程序中使用StringBulider(线程不安全)代替StringBuffer
*StringBuffer中的常用方法
* 构造方法:
*public StringBuffer(){ }
构造一个不带字符的字符串缓冲区,该字符串缓冲区的初始容量为16个字符
*public StringBuffer(int capacity)
构造一个不带字符,但具有指定初始容量的字符串缓冲区
*public StringBuffer(String str)
构造一个字符串缓冲区,并将其内容初始化为指定的字符串内容。该字符串的初 始容量为 16 加上字符串参数的长度。
* public int length();获取字符串长度
* public int capacity():获取当前字符串缓冲区的容量
*StringBuffer的添加方法:
* public StringBuffer append( );向缓冲区追加数据,返回字符串缓冲区本身
* public StringBUffer insert(int offset,String str):在指定位置添加一个 字符串,返回字符串缓冲区本身
*StringBuffer的删除方法:
* public StringBuffer deleteCharAt(int start):删除指定位置的字符,返回 字符串缓冲区本身
* public StringBuffer deleteCharAt(int start,int end):删除从指定位置开 始到指定位置结束的字符返回 字符串缓冲区本身
package stringbuffer;
public class StringBufferDemo {
public static void main(String[] args ){
//StringBuffer不可以像String一样直接赋值,可以创建对象利用构造方法赋值
//也可以通过无参构造,调用append();方法进行添加
StringBuffer sb=new StringBuffer();
sb.append("hello");
sb.append("world");
sb.append("-----javase");
System.out.println("添加初始的字符串:"+sb);
sb.insert(5, "-----");
System.out.println("用insert()添加:"+sb);
System.out.println("----------------");
sb.deleteCharAt(3);
System.out.println("删除一个字符后的字符串:"+sb);
System.out.println("----------------");
//当前索引所对应的字符一发生了改变
sb.delete(1, 3);
System.out.println("删除一个小字符串后:"+sb);
System.out.println("----------------");
}
}*StringBuffer和String之间的转换
*StringBuffer的反转功能:
public StringBuffer reserve();将字符串顺序直接翻转
package stringbuffer;
public class StringBufferDemo1 {
public static void main(String[] args) {
String str="hello";
//String----->StringBuffer的转换
System.out.println("String----->StringBuffer的转换");
//创建StringBuffer的对象指向这个字符串
StringBuffer sb=new StringBuffer(str);
System.out.println("创建StringBuffer的对象指向这个字符串:"+sb);
//使用无参构造,调用append()方法
StringBuffer sb1=new StringBuffer();
sb1.append(str);
System.out.println("使用无参构造,调用append()方法:"+sb1);
System.out.println("----------------");
//StringBuffer----->String的转换
System.out.println("StringBuffer----->String的转换");
//创建缓冲区对象
StringBuffer sb2=new StringBuffer("world");
//String有一种构造方法String(StringBuffer str)
String sb3=new String(sb2);
System.out.println("构造方法String(StringBuffer str):"+sb3);
//toString()方法
String sb4=sb2.toString();
System.out.println("toString()方法:"+sb4);
System.out.println("----------------");
//使用StringBuffer的翻转功能将字符串反转“reverse()”
System.out.println("String类型“hello”使用reverse()翻转后:"+sb2.reverse().toString());
}
}
*StringBufferd的替换功能:
*public StringBuffer replace(int start,int end,String str):从指定位置开始到指定位 置用str替换
*StringBuffer的截取功能:
*public StringBuffer substring(int start):从指定位置开始到末尾结束,返回一个新的字 符串
*public StringBuffer substring(int start,int end):从指定位置开始到指定位置结束,返 回一个新的字符串
package stringbuffer;
public class StringBufferDemo2 {
public static void main(String[] args){
StringBuffer sb=new StringBuffer();
sb.append("helloworld");
System.out.println("初始字符串:"+sb);
System.out.println("替换后:"+sb.replace(0, 5, "hi-"));
System.out.println("------------------");
//StringBuffer的截取功能
//从指定位置开始截取,默认到结尾结束
System.out.println("从的指定位置往后截取:"+sb.substring(5));
System.out.println("从指定位置开始到指定位置结束:"+sb.substring(3, 6));
}
}问题:
*String、StringBuffer、StringBuleder的区别
String: 一个不可变的字符序列
StringBuffer和StringBuleder是可变字符序列,在单线程时优先采用StirngBuleder
*从线程角度考虑:StringBuleder线程不安全,不同步,执行效率高,比StringBuffer快
*String和StringBuffer作为形式参数和基本类型是一样的
*Integer类
jdk5.0之后的新特性:自动拆装箱,可变参数……
每个基本数据类型都会被封装成一个引用类型
基本类型 引用类型
int Integer
char Character
byte Byte
long Long
double Double
short Short
float Float
boolean Blooean
Integer类的构造方式:
public Integer(int value):将一个int类型的数据封装成一个引用类型
public Integer(String s):将一个字符数类型封装成一个Integer类型
该字符串必须是数字字符串否则运行出错:java.lang.NumberFormatException
package integer;
public class IntegerDemo {
public static void main(String[] args) {
//public static String toBinaryString(int i)
//以二进制无符号数形式返回一个整数参数的字符串表示形式
System.out.println(Integer.toBinaryString(100)) ;
//public static String toOctalString(int i)
//以八进制无符号数形式返回一个整数参数的字符串表示形式
System.out.println(Integer.toOctalString(100));
//public static String toHexString(int i)
//以十六进制无符号数形式返回一个整数参数的字符串表示形式
System.out.println(Integer.toHexString(100));
//public static final int MAX_VALUE
//int类型能够表示的最大值
//public static final int MIN_VALUE
//int类型所能表示的最小值
System.out.println(Integer.MAX_VALUE);//2147483647
System.out.println(Integer.MIN_VALUE);//-2147483647
}
}
*int类型和String类型的转换
*public int intValue();//以int类型返回该Integer的值
*public static int parseInt(String str);//将字符串参数作为有符号数的十进制整数进行 解析
*Character类
*在对象中包装一个基本类型char的值,Character类型的对象包含类型为char的字段
*构造方法:
public Character(char value)://构造一个新分配的Character对象,来表示制定的 char的值
*Character的判断功能和转换功能
public static boolean isLowerCase(char ch)确定指定字符是否为小写字母。
*public static boolenn isUpperCase(char ch)确定指定字符是否为大写字母
*public static boolean isDigit(char ch)确定指定字符是否为数字。
*public static char toUpperCase(char ch):将指定字符转换成大写
*public static char toLowerCase(char ch):将指定字符转换成小写
package character;
public class CharacterDemo {
public static void main(String[] args) {
//public static boolean isLowerCase(char ch)确定指定字符是否为小写字母
System.out.println("isLowerCase:"+Character.isLowerCase(‘a‘));
System.out.println("isLowerCase:"+Character.isLowerCase(‘A‘));
System.out.println("isLowerCase:"+Character.isLowerCase(‘0‘));
System.out.println("---------------------------------------");
//public static boolenn isUpperCase(char ch)确定指定字符是否为大写字母
System.out.println("isUpperCase:"+Character.isUpperCase(‘a‘));
System.out.println("isUpperCase:"+Character.isUpperCase(‘A‘));
System.out.println("isUpperCase:"+Character.isUpperCase(‘0‘));
System.out.println("---------------------------------------");
//public static boolean isDigit(char ch)确定指定字符是否为数字。
System.out.println("isDigit:"+Character.isDigit(‘a‘));
System.out.println("isDigit:"+Character.isDigit(‘A‘));
System.out.println("isDigit:"+Character.isDigit(‘0‘));
}
}
原文地址:http://13355940.blog.51cto.com/13345940/1978822