字符串和字符

来源:互联网 发布:声道测试软件 编辑:程序博客网 时间:2024/05/20 09:26

String提供9个构造函数,以不同的方式来初始化String对象。以下代码展示了六个方法:

 

public class StringConstructors
{
    
public static void main(String args[]){
        
char charArray[] = {'b','i','r','t','h',' ','d','a','y'};
        
byte byteArray[] = {(byte)'n',(byte)'e',(byte)'w',(byte)' ',(byte)'y',(byte)'e',(byte)'a',(byte)'r'};
        String s 
= new String("Hello");

        String s1 
= new String();
        String s2 
= new String(s);
        String s3 
= new String(charArray);
        String s4 
= new String(charArray,6,3);
        String s5 
= new String(byteArray,4,4);
        String s6 
= new String(byteArray);

        String output 
= "s1 = " + s1 + " s2 = " + s2 + " s2 = " + s2 + " s3 = " + s3 + " s4 = " + s4 + " s5 = " + s5 + " s6 = " + s6 ;

        JOptionPane.showMessageDialog(
null,output,
            
"String Class Constructors", JOptionPane.INFORMATION_MESSAGE);
        System.exit(
0);
    }

}
;

 

1,字符转换:Integer.parseInt();

2,字符串连接:String s = new String("Good");

                           String u = new String("Night");

                           System.out.println(s.concat(u));

3,大小写转换:String toLowerCase();            String toUpperCase();

4,删除首位空格:String trim();

5,字符串反转:StringBuffer s  = new StringBuffer("123456");

                               System.out.println(s.reverse());

6,将StringBuffer转换成String;

       StringBuffer s = new StringBuffer("abcdeg");

       String s2 = s.toString();

 

原创粉丝点击