字符串和字符

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();

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值