Note that you're question is not very precise and hence your requirements are not very clear.
Simple way :
String st = new StringBuilder(s).substring(3,6);
Or you can directly construct the new String using reflection to get the char array:
public static String substring(String s, int from, int to){
Field f = String.class.getDeclaredField("value");
f.setAccessible(true);
char [] tab = (char[])f.get(s);
f.setAccessible(false);
return new String(tab, from, to - from);
}
Those can also be options (note that it works only if the original
String fits an hexadecimal format) :
String s ="abcfedbca";
BigInteger bi = new BigInteger(s, 16);
bi = bi.and(new BigInteger("16699392")); //0xfed000 in hexa
bi = bi.shiftRight(12);
System.out.println(bi.toString(16));
Or more simple :
String s ="abcfedbca";
System.out.println(Integer.toHexString((int)(Long.parseLong(s, 16) & 16699392) >> 12));
If you want a more general method, this one might suits your case :
public static void main(String [] args){
String s ="abcfedbca";
System.out.println(substring(s, 2, 5, 9));
}
public static String substring (String s, int from, int to, int length){
long shiftLeft = 0;
long shiftRight = (length - to - 1) * 4;
for(int i = 0; i < to - from - 1; i++){
shiftLeft += 15;
shiftLeft = shiftLeft << 4;
}
shiftLeft += 15;
return Long.toHexString((Long.parseLong(s, 16) & (shiftLeft << shiftRight)) >> shiftRight);
}
public String subString(String str, int i, int j) { p = Runtime.getRuntime().exec("echo " + str + "| cut -c"+i+"-"+j); p.waitFor(); return new BufferedReader(new InputStreamReader(p.getInputStream())).readLine();}(*NIX only)