截取字符串,只截取前N个字节的字符

阅读 36

2023-04-12


   public  void cutString(String str, int n)
  {
      String temp;
       int count=0;
       for ( int i = 0; i < str.length(); i++) {
        temp=str.charAt(i)+"";
        count+=temp.getBytes().length;
         if(count>n)
             break;
        System.out.print(temp);
    }
  }
  @Test
   public  void testString()
  {
      cutString("我ABC好的sfd", 7);
  }
————————————————

精彩评论(0)

0 0 举报