Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中从字符串生成子字符串而不使用任何字符串函数_Java - Fatal编程技术网

在java中从字符串生成子字符串而不使用任何字符串函数

在java中从字符串生成子字符串而不使用任何字符串函数,java,Java,我最近在一次采访中被问到这个问题(3+经验)。不允许使用字符串函数,包括String.length。我有5分钟的时间来写完整的代码 示例 String ="abcfedbca" subString = "fed" 事实上,我让他再详细说明一下,他说“java实现很简单,我应该详细说明什么”,基于这个问题,他只说我的核心java非常弱 您可以使用StringBuilder并对其调用substring。不知道这是否被允许 这是另一种方式(尽管我为这种解决方案感到羞耻): 您可以使用StringB

我最近在一次采访中被问到这个问题(3+经验)。不允许使用字符串函数,包括
String.length
。我有5分钟的时间来写完整的代码

示例

String ="abcfedbca"
subString = "fed" 

事实上,我让他再详细说明一下,他说“java实现很简单,我应该详细说明什么”,基于这个问题,他只说我的核心java非常弱

您可以使用
StringBuilder
并对其调用
substring
。不知道这是否被允许

这是另一种方式(尽管我为这种解决方案感到羞耻):


您可以使用
StringBuilder
并对其调用
substring
。不知道这是否被允许

这是另一种方式(尽管我为这种解决方案感到羞耻):

这个怎么样


这个怎么样?

StringBuilder对我来说似乎是个骗局,因为它是基于字符串的。我认为您必须使用字节原语来满足测试的精神。当然,这会调用
getBytes
,从技术上讲,这是一个字符串函数,因此也会破坏它

public static void main(String[] args) {
    String full ="abcfedbca";
    String desiredsubString = "fed";

    byte[] fullByte = full.getBytes();  
    byte[] desiredByte = desiredsubString.getBytes();
    byte[] foundStorage = new byte[desiredByte.length];

    boolean foundStart = false;
    int inserted = 0;

    for (byte b : fullByte) {
        if ( !foundStart && (b == desiredByte[0]) )
        {
            foundStorage[0] = b;
            inserted++;
            foundStart = true;
        }
        else if (foundStart && (inserted < foundStorage.length))
        {
            foundStorage[inserted] = b;
            inserted++;

            if ( inserted >= foundStorage.length )
            {
                break;
            }
        }
    }

    System.out.println("These should be equal: " + new String(desiredByte).equals(new String(foundStorage)));

}
publicstaticvoidmain(字符串[]args){
String full=“abcfedbca”;
字符串desiredsubString=“fed”;
byte[]fullByte=full.getBytes();
byte[]desiredByte=desiredsubString.getBytes();
byte[]foundStorage=新字节[desiredByte.length];
布尔值foundStart=false;
插入的int=0;
for(字节b:全字节){
如果(!foundStart&&(b==desiredByte[0]))
{
foundStorage[0]=b;
插入++;
foundStart=true;
}
else if(foundStart&(插入=foundStorage.length)
{
打破
}
}
}
System.out.println(“这些应该相等:“+新字符串(desiredByte).equals(新字符串(foundStorage)));
}

StringBuilder对我来说似乎是个骗局,因为它是基于字符串的。我认为您必须使用字节原语来满足测试的精神。当然,这会调用
getBytes
,从技术上讲,这是一个字符串函数,因此也会破坏它

public static void main(String[] args) {
    String full ="abcfedbca";
    String desiredsubString = "fed";

    byte[] fullByte = full.getBytes();  
    byte[] desiredByte = desiredsubString.getBytes();
    byte[] foundStorage = new byte[desiredByte.length];

    boolean foundStart = false;
    int inserted = 0;

    for (byte b : fullByte) {
        if ( !foundStart && (b == desiredByte[0]) )
        {
            foundStorage[0] = b;
            inserted++;
            foundStart = true;
        }
        else if (foundStart && (inserted < foundStorage.length))
        {
            foundStorage[inserted] = b;
            inserted++;

            if ( inserted >= foundStorage.length )
            {
                break;
            }
        }
    }

    System.out.println("These should be equal: " + new String(desiredByte).equals(new String(foundStorage)));

}
publicstaticvoidmain(字符串[]args){
String full=“abcfedbca”;
字符串desiredsubString=“fed”;
byte[]fullByte=full.getBytes();
byte[]desiredByte=desiredsubString.getBytes();
byte[]foundStorage=新字节[desiredByte.length];
布尔值foundStart=false;
插入的int=0;
for(字节b:全字节){
如果(!foundStart&&(b==desiredByte[0]))
{
foundStorage[0]=b;
插入++;
foundStart=true;
}
else if(foundStart&(插入=foundStorage.length)
{
打破
}
}
}
System.out.println(“这些应该相等:“+新字符串(desiredByte).equals(新字符串(foundStorage)));
}
也许:

    String value = "abcfedbca";
    BigInteger bi = new BigInteger(value, 16);
    bi = bi.divide(BigInteger.TEN);
    bi = bi.divide(BigInteger.TEN);
    bi = bi.divide(BigInteger.TEN);
    bi = bi.divide(BigInteger.TEN);
    bi = bi.divide(BigInteger.TEN);
    bi = bi.divide(BigInteger.TEN);
    bi = bi.divide(BigInteger.TEN);     
    int val = bi.subtract(BigInteger.valueOf(535)).intValue();
    String hex = Integer.toHexString(val);  

    System.out.println(hex);
输出:
fed

可能:

    String value = "abcfedbca";
    BigInteger bi = new BigInteger(value, 16);
    bi = bi.divide(BigInteger.TEN);
    bi = bi.divide(BigInteger.TEN);
    bi = bi.divide(BigInteger.TEN);
    bi = bi.divide(BigInteger.TEN);
    bi = bi.divide(BigInteger.TEN);
    bi = bi.divide(BigInteger.TEN);
    bi = bi.divide(BigInteger.TEN);     
    int val = bi.subtract(BigInteger.valueOf(535)).intValue();
    String hex = Integer.toHexString(val);  

    System.out.println(hex);

输出:
fed

请注意,您的问题不是很精确,因此您的要求不是很清楚

简单方法:

String st = new StringBuilder(s).substring(3,6); 
或者您可以使用反射直接构造新的
字符串
,以获得
字符
数组:

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);
}

这些也可以是选项(请注意,只有当原始
字符串符合十六进制格式时,它才起作用):

或者更简单:

String s ="abcfedbca";
System.out.println(Integer.toHexString((int)(Long.parseLong(s, 16) & 16699392) >> 12));

如果您想要更通用的方法,此方法可能适合您的情况:

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);
  }
publicstaticvoidmain(字符串[]args){
字符串s=“abcfedbca”;
System.out.println(子字符串(s,2,5,9));
}
公共静态字符串子字符串(字符串s、int-from、int-to、int-length){
长移位英尺=0;
长移位tright=(长度-到-1)*4;
for(int i=0;i
请注意,您的问题不是很精确,因此您的要求不是很清楚

简单方法:

String st = new StringBuilder(s).substring(3,6); 
或者您可以使用反射直接构造新的
字符串
,以获得
字符
数组:

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);
}

这些也可以是选项(请注意,只有当原始
字符串符合十六进制格式时,它才起作用):

或者更简单:

String s ="abcfedbca";
System.out.println(Integer.toHexString((int)(Long.parseLong(s, 16) & 16699392) >> 12));

如果您想要更通用的方法,此方法可能适合您的情况:

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);
  }
publicstaticvoidmain(字符串[]args){
字符串s=“abcfedbca”;
System.out.println(子字符串(s,2,5,9));
}
公共静态字符串子字符串(字符串s、int-from、int-to、int-length){
长移位英尺=0;
长移位tright=(长度-到-1)*4;
for(int i=0;i
使用JPasswordField获取字符怎么样:

private static String substring(String str, int beginIndex, int endIndex) {
    return new String(new JPasswordField(str).getPassword(), beginIndex, endIndex - beginIndex);
}

字符串构造函数不是字符串方法,不是吗?

使用JPasswordField获取字符怎么样:

private static String substring(String str, int beginIndex, int endIndex) {
    return new String(new JPasswordField(str).getPassword(), beginIndex, endIndex - beginIndex);
}
public String submethod(String data, int firstindex, int lastindex) {
        char[] chars=data.toCharArray();
        char[]charnew= new char [lastindex];
        for(int i=firstindex; i< lastindex;i++) {
            charnew[i]=chars[i];
        }
        return new String(charnew); 
    }
字符串构造函数不是字符串方法,不是吗?

公共字符串子方法(字符串数据,int firstindex,int lastindex){
public String submethod(String data, int firstindex, int lastindex) {
        char[] chars=data.toCharArray();
        char[]charnew= new char [lastindex];
        for(int i=firstindex; i< lastindex;i++) {
            charnew[i]=chars[i];
        }
        return new String(charnew); 
    }
char[]chars=data.toCharArray(); char[]charnew=新字符[lastindex]; for(int i=firstindex;i
公共字符串子方法(字符串数据、int-firstindex、int-lastindex){
char[]chars=data.toCharArray();
char[]charnew=新字符[lastindex];
for(int i=firstindex;i