一道关于字符串截取的面试题,字符串截取面试题
分享于 点击 6395 次 点评:68
一道关于字符串截取的面试题,字符串截取面试题
package facing;import java.io.UnsupportedEncodingException;
public class SubString {
public static void main(String[] args) throws UnsupportedEncodingException {
SubString s=new SubString();
String n=s.getString("我abc汗b人家", 4);
System.out.println(n);
String q="我是asd人家";
byte [] a=q.getBytes();
System.out.println(a[4]);
}
public String getString(String s,int n) throws UnsupportedEncodingException{
int index = 0; //定义指針的位置
StringBuffer ss = new StringBuffer();
for(int i = 0 ; i < n-1; i++){
//判断是否为数字或者字母,然后指针向前移动一位
if(s.charAt(index)<255 && s.charAt(index)>0 || Character.isDigit(s.charAt(index))){
ss.append(s.charAt(index));
index++;
}else{
ss.append(s.charAt(index)); //如果当前字符是汉字,则添加到结果中,指针向前移动一位。
index++;
}
}
return ss.toString();
}
}
相关文章
- 暂无相关文章
用户点评