首页 > 试题广场 >

实现一个按字节来截取字符串的子串的方法,功能类似于Strin

[问答题]
实现一个按字节来截取字符串的子串的方法,功能类似于String类的substring()方法, String类是按字符截取的,例如”中国abc", substring(1,3), 将返回"国a"。这里要求按字节截取,一个英文字符当一个字节,一个中文字符当两个字节。
//以下仅供参考
  public void substring(String str, int toCount){
        int reInt = 0;
        String reStr = "";
        char[] tempChar = str.toCharArray();
        for (int kk = 0; (kk < tempChar.length && toCount > reInt); kk++){        
            String s1 = str.valueOf(tempChar[kk]);
            byte[] b = s1.getBytes();
            reInt += b.length;    //汉字的length为2
            reStr += tempChar[kk];
        }
        System.out.println(reStr);


发表于 2018-11-08 19:48:22 回复(0)
更多回答

public class Demo01 {
 public static void main(String[] args) {   System.out.println(Demo01.StringCut("中国你好abcdef", 0, 7));  }    public static String StringCut(String str,int from,int to){   byte[] a = str.getBytes();   byte[] b = new byte[to-from];   //将a数组里从索引为from的元素开始,   //复制到数组b里的索引为0的位置, 复制的元素个数为to-from个.   System.arraycopy(a, from, b, 0, to-from);   String newStr = new String(b);   return newStr;  } }
输入结果:中国你?


发表于 2018-12-03 15:57:57 回复(0)
public class InterceptString {
    public static void main(String args[]) {
        InterceptString is = new InterceptString();
        String str = "少一些功利***的追求,多一些不为什么的坚持!" ;
        System.out.println(is.stringCut(str,0,10));
        
    }
    public String stringCut(String str,int start,int end) {
        byte[] strByte = str.getBytes();
        byte[] newstrByte = new byte[end - start];
        System.arraycopy(strByte, start, newstrByte, 0, end - start);
        String cutStr = new String(newstrByte);
        return cutStr;
    }
}

发表于 2019-04-02 20:58:18 回复(0)
 public String mySubString(String inputString, int from, int to) throws UnsupportedEncodingException {
  // TODO Auto-generated method stub
  byte[] bytes = inputString.getBytes("UTF-8"); 
  
  int size = to;
  if(to > bytes.length)
  {
   size = bytes.length;
  }
  
  byte[] resultBytes = new byte[size - from];
  
  int j = 0;
  for (int i = from; i < size; i++) {
   resultBytes[j] = bytes[i];
   j++;
  }
  
  return new String(resultBytes, "UTF-8");
 }

发表于 2018-11-09 14:53:38 回复(0)
   public static void main(String[] args) throws UnsupportedEncodingException {
        String str1 = "asd阿达";
        int start = 2;
        int end = 5;
        byte[] bytes = str1.getBytes("GBK");
        byte[] newByte = new byte[end - start];
        System.arraycopy(bytes, start,newByte, 0, end-start);
        String newStr = new String(newByte, "GBK");
        System.out.println(newStr);
    }


发表于 2021-03-19 17:17:49 回复(0)
public class Test3 {
    public static void main(String[] args){
    String str = "我们的梦想是星辰大海java!";
    System.out.println(stringCut(str,4,12));
    }
    public static String stringCut(String str,int start,int end){
        byte[] strBytes = str.getBytes();
        byte[] newBytes = new byte[end-start];
        int j=0;
        for(int i=start;i<end;i++){
            newBytes[j++] = strBytes[i];
        }
        String newStr = new String(newBytes);
        return newStr;
    }
}

发表于 2020-05-02 10:27:15 回复(0)
import java.util.Scanner;
public class Practice{
 public static void main(String[] args) {
  Scanner sc=new Scanner(System.in);
  String str=sc.next();
  int a=sc.nextInt();
  int b=sc.nextInt();
  System.out.println(cute(str,a,b));
 }
 public static String cute(String str,int start,int end) {
  byte[] bytes=str.getBytes();
  byte[]b=new byte[1024];
  System.arraycopy(bytes, start,b , 0, end-start);
  return new String(b);
 }
}
发表于 2020-03-04 15:43:02 回复(0)
public class Main { public static void main(String[] args) {
        String str = "中国加油!";  System.out.println(bytecut(str,2,10));   } public static String bytecut(String str,int starttId, int endId){ byte[] bytestr = str.getBytes();  byte[] newbytestr = new byte[endId-starttId];  for(int i=1;i<=endId-starttId;i++){
            newbytestr[i-1]=bytestr[i+starttId-1];  }
        String newstr = new String(newbytestr);  return newstr;  }
}
发表于 2020-01-27 17:23:17 回复(0)
import java.util.*;
public class SubString_1 {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        System.out.println("请要截取的输入字符串:");
        String str=in.next();
        System.out.println("请输入要截取的字节的首尾上标");
        int fromIndex=in.nextInt();
        System.out.println("请输入要截取的字节的首尾下标");
        int toIndex=in.nextInt();
        SubString_1 sp=new  SubString_1();
        System.out.println(sp.cutString(str, fromIndex, toIndex));
        
    }
    public String cutString(String str,int fromIndex,int toIndex) {
        String str2="";
        String str3="";
        for(int i=fromIndex;i<=toIndex;) {
            str2=str2+str.charAt(i);
            int k=str2.getBytes().length;
            str3=str2+str.charAt(i+1);
            int s=str3.getBytes().length;
            if(fromIndex+k-1<toIndex&&fromIndex+s-1<=toIndex) {
                i++;//目前状态下和后一个状态都要考虑
            }else {
                break;
            }
        }
        return str2;
    }
}
发表于 2019-12-28 22:43:58 回复(0)

public class CalendarB {

    

    public static String  cut(String c ,int b, int a){
        byte[]  s  =c.getBytes();
        byte[] s1 = new byte[a-b];
             
        System.arraycopy(s, b, s1, 0, a-b);
        String s2 = new String(s1);
        return s2;
                }
    public static void main(String [] args){
      
        System.out.println(cut("改革春风吹满地,中国人民真争气",4,10));
      
        
    }
    }

发表于 2019-03-27 14:32:18 回复(0)