您的位置:首页 > 编程语言 > Java开发

java学习笔记五--String类与String例子

2013-03-03 12:41 495 查看
请解释字符串比较之中“==”和equals()的区别?

         ==:比较的是两个字符串内存地址的数值是否相等,属于数值比较;

        equals():比较的是两个字符串的内容,属于内容比较。

        以后进行字符串相等判断的时候都使用equals()

字符串常量就是一个匿名对象,匿名对象永远不可能为null

在String类进行设计的时候采用了一种称为共享设计模式的概念,在每一个运行的JVM底层存在一个字符串的对象池(Object Pool),如果用户采用了直接赋值的方式,会将字符串的内容放入到池之中,以供其他继续使用直接赋值方式的String对象使用,如果新声明的字符串内容不在池之中,则会开辟一个新的,继续放到池,以供下次使用。

   
public class StringDemo {

/**
* @param args
*/
public static void main(String[] args) {
String a="hello";
String b="hello";
String c="hello";
String d=new String("hello");
System.out.println(a==b);
System.out.println(a==c);
System.out.println(b==c);
System.out.println(d==a);
}

}
 使用构造方法的方式开辟的字符串对象,实际上会开辟两块空间,其中有一块空间将称为垃圾。
public class StringDemo {
public static void main(String args[]) {
String str1 = new String("Hello") ;
String str2 = "Hello" ;  // 入池
String str3 = "Hello" ;  // 使用池对象
System.out.println(str1 == str2) ;    // false
System.out.println(str1 == str3) ;    // false
System.out.println(str2 == str3) ;    // true
}
}
通过上面的程序可以发现,使用构造方法实例化的String对象,不会入池,所以,只能自己使用。可是在String类之中为了方便操作提供了一种称为手工入池的方法:public String intern()。
public class StringDemo {
public static void main(String args[]) {
String str1 = new String("Hello").intern() ;
String str2 = "Hello" ;  // 入池
String str3 = "Hello" ;  // 使用池对象
System.out.println(str1 == str2) ;    // true
System.out.println(str1 == str3) ;    // true
System.out.println(str2 == str3) ;    // true
}
}
面试题:请解释String类的两种对象实例化方式的区别?

         · 直接赋值:只开辟一块堆内存空间,字符串的内容可以自动入池,以供下次使用;

         · 构造方法:开辟两块堆内存空间,有一块将成为垃圾,并且不能自动入池,使用intern()手工入池。

         在日后的所有开发之中,String对象的实例化永远都采用直接赋值的方式完成。

 
字符串内容的更改,实际上改变的是字符串对象的引用过程,并且会伴随有大量的垃圾出现,那么对于以下的代码实际之中应该避免:
public class StringDemo {
public static void main(String args[]) {
String str = "" ;
for (int x = 0 ; x < 1000 ; x ++) {
str += x ;
}
System.out.println(str) ;
}
}
但是这种代码需要“断开-连接”String对象1000次,会产生大量垃圾,所以不能够去使用。

 

public String(char[] value) 构造 将全部的字符数组内容变为字符串

public String(char[] value, int offset, int count) 构造 将部分字符数组变为字符串,offset表示开始点,count表示要操作的长度
public char charAt(int index) 普通 取得指定索引位置上的字符
public char[] toCharArray() 普通 将字符串转换为字符数组

字符串和字符数组转换,完成一个小写字符串变为大写字符串的操作,小写字母和大写字母差了32
public class StringDemo {
public static void main(String args[]) {
String str = "helloworld" ;
char data [] = str.toCharArray() ;     // 字符串变为字符数组
for (int x = 0 ; x < data.length ; x ++) {
System.out.print(data[x] + "、") ;
data [x] -= 32 ;    // 变大写
}
System.out.println() ;
System.out.println("全部字符数组变为字符串:" + new String(data)) ;
System.out.println("部分字符数组变为字符串:" + new String(data,0,5)) ;
}
}

现在要求判断一个字符串是否由数字所组成
public class StringDemo {
public static void main(String args[]) {
char c = '8' ;
System.out.println(c >= '0' && c <= '9') ;
System.out.println((int) c) ;
}
}

将一个字符串首先变为字符数组,而后依次判断字符数组之中的每一个字符是否是数字,如果全是,则返回true,否则返回false
public class StringDemo {
public static void main(String args[]) {
String str = " 1a 23" ;
System.out.println(isNumber(str)) ;
}
public static boolean isNumber(String temp) {
char data [] = temp.toCharArray() ;    // 变为字符数组
for (int x = 0 ; x < data.length ; x ++) {
if (data[x] < '0' || data[x] > '9') {
return false ;      // 不是数字
}
}
return true ;
}
}


public String(byte[] bytes) 构造 将全部的字节数组变为字符串

public String(byte[] bytes, int offset, int length) 构造 将部分的字节数组变为字符串

public byte[] getBytes() 普通 将字符串变为字节数组

public byte[] getBytes(String charsetName) throws UnsupportedEncodingException 普通 字符串转码操作

 

一般情况下,在程序之中如果要想操作字节数组只有两种情况:

                   · 情况一:需要进行编码的转换时;

                   · 情况二:数据要进行传输的时候。

   

public boolean equals(String anObject) 普通 区分大小写的相等判断

public boolean equalsIgnoreCase(String anotherString) 普通 不区分大小写比较是否相等

public int compareTo(String anotherString) 普通 比较两个字符串的大小

public boolean contains(String s) 普通 查找指定的子字符串是否存在,JDK 1.5之后有

public int indexOf(String str) 普通 从头查找指定字符串的位置,找不到返回-1

public int indexOf(String str, int fromIndex) 普通 由指定位置向后查找字符串的位置,找不到返回-1

public int lastIndexOf(String str) 普通 由后向前查找字符串的位置,找不到返回-1

public int lastIndexOf(String str, int fromIndex) 普通 从指定位置由后向前查找

public boolean startsWith(String prefix) 普通 判断是否以指定的字符串开头

public boolean startsWith(String prefix, int toffset) 普通 从指定位置判断是否以指定字符串开头,JDK 1.7

public boolean endsWith(String suffix) 普通 判断是否以指定的字符串结尾

public String replaceAll(String regex, String replacement) 普通 全部替换

public String replaceFirst(String regex, String replacement) 普通 替换首个

public String substring(int beginIndex) 普通 从指定位置截取到结尾

public String substring(int beginIndex, int endIndex) 普通 截取指定范围的内容

public String[] split(String regex) 普通 按照指定的字符串全拆分

public String[] split(String regex, int limit) 普通 拆分为指定的长度

public class StringDemo {
public static void main(String args[]) {
String str = "192.168.1.1" ;
String result []= str.split("\\.") ;
for (int x = 0 ; x < result.length ; x ++) {
System.out.println(result[x]) ;
}
}
}


public boolean isEmpty() 普通 判断是否为空字符串("")

public int length() 普通 取得字符串长度

public String trim() 普通 去掉左右空格

public String toLowerCase() 普通 将全部字符串转小写

public String toUpperCase() 普通 将全部字符串转大写

public String intern() 普通 入池

public String concat(String str) 普通 字符串连接

可以让首字母大写,可是这个方法其实很重要,但是String类没有提供。下面简单实现一下,给一个基本的原理。
public class StringDemo {
public static void main(String args[]) {
String str = "hello" ;
System.out.println(initcap(str)) ;
}
public static String initcap(String s) {
return s.substring(0,1).toUpperCase().concat(s.substring(1)) ;
}
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: