String类常用方法之charAt()、codePointAt()示例

1、chatAt()——提取指定字符串

2、codePointAt()——提取索引字符代码点

 

Java代码

  1. public class StringDemo {   
  2.     public static void main(String[] args) {   
  3.         String str1 = "abcdefg";   
  4.         char ch1 = str1.charAt(0);   
  5.         System.out.println("使用charAt()方法" +   
  6.                 "从字符串中提取字符,结果是:" + ch1);   
  7.         int codePoint = 0;   
  8.         for(int i = 0 ; i < 8 ; i ++){   
  9.             try{   
  10.                 codePoint = str1.codePointAt(i);   
  11.             }catch(StringIndexOutOfBoundsException e1){   
  12.                 System.out.println("codePointAy()所调用的索引值" + i +    
  13.                         "已经超出所要查询的字符串的长度!");   
  14.             }finally{   
  15.                 try{   
  16.                     System.out.println(str1.charAt(i)    
  17.                             + "的Unicode码为" + ":" + codePoint);   
  18.                 }catch(StringIndexOutOfBoundsException e2){   
  19.                     System.out.println("charAt()所调用的索引值" + i +    
  20.                             "已经超出所要查询的字符串的长度!");   
  21.                 }   
  22.                    
  23.             }   
  24.         }   
  25.            
  26.            
  27.     }   
  28.   
  29. }   
  30. /*out:  
  31.  使用charAt()方法从字符串中提取字符,结果是:a  
  32. a的Unicode码为:97  
  33. b的Unicode码为:98  
  34. c的Unicode码为:99  
  35. d的Unicode码为:100  
  36. e的Unicode码为:101  
  37. f的Unicode码为:102  
  38. g的Unicode码为:103  
  39. codePointAy()所调用的索引值7已经超出所要查询的字符串的长度!  
  40. charAt()所调用的索引值7已经超出所要查询的字符串的长度!*/  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值