简体   繁体   English

从java中的字符串中删除字符

[英]Removing characters from string in java

Removing characters between single quotes. 删除单引号之间的字符。 I am trying this. 我正在尝试这个。

If I entered sa' or '1'='1 then output should be ' or ' '=' . 如果我输入sa' or '1'='1则输出应为' or ' '=' I am working on sql injection project. 我正在研究sql注入项目。 What they require is removing characters from single quotes. 他们需要的是从单引号中删除字符。

By using prepared statement we can prevent injection. 通过使用预备语句,我们可以防止注入。 But before that I want to remove characters between single quotes. 但在此之前,我想删除单引号之间的字符。 How to do this. 这个怎么做。 Is there any easy way. 有没有简单的方法。

StringBuilder strBuilder = new StringBuilder();
String [] ary = uname.split("");
int j = 1;
for (int i = 0 ; i < ary.length ; i++) {
    if (ary[i].equals("'")) {
        if (j == 1) {
            strBuilder = new StringBuilder();
            strBuilder.append(ary[i]);
            j++;
        }
        else if (j % 2 == 0) {
            strBuilder.append(ary[i]);
            j++;
        }
        else if (j % 3 == 0) {
            strBuilder.append(ary[i]);
        }
        else if (j % 4 == 0) {
            strBuilder.append(ary[i]);
            break;
        }
    }
    else {
        strBuilder.append(ary[i]);
    }
}
uname = strBuilder.toString();
System.out.println("uname: " + uname);

You could do something like this, read each char one by one in the string in a loop and check if that character is ' If it is then keep on deleting the following characters in the string until the ' character comes again... 你可以做这样的事情,在循环中的字符串中逐个读取每个char ,并检查该字符是否为'如果是,那么继续删除字符串中的以下字符,直到'字符再次出现...

The pseudo code will be like this: 伪代码将是这样的:

char string(10)="Da`ks`H"
for(int x=0, x<string_length, x++){
    if(string[x]=='){
        while(string[x]!='){
            delete
        }
    }
}

and if you are wondering how you can delete then you can do one thing: make a new string and in that copy the chars if the chars are not between ' so in this case it would be like: 如果你想知道如何删除,那么你可以做一件事:创建一个新的字符串,如果字符不在'之间,那就是字符副本'所以在这种情况下它会是这样的:

char string(10)="Da`ks`H"
char newstring(10)
for(int x=0, x<string_length, x++){
    if(string[x]=='){
        while(string[x]!='){
            delete
        }
    }
    else{
        string[x]=newstring[x]
    }
}

Note: Above (^) I am not showing the actual code but it is just to make you understand the logic, syntax does not belong to any language 注意:上面(^)我没有显示实际代码,但它只是让你理解逻辑,语法不属于任何语言

Not quite sure what you are doing with your code . 不太清楚你在使用代码做什么。 Code will never enter else if(j%4==0) block because if this condition is true then else if(j%2==0) will obviously be true. else if(j%4==0)阻塞,代码永远不会输入else if(j%4==0)因为如果这个条件为真,那么else if(j%2==0)显然是真的。

If you just want to skip characters between single quotes use a boolean flag. 如果您只想跳过单引号之间的字符,请使用布尔标志。 Set it to true at index it is encountered and set it to false it is encountered next. 在遇到索引时将其设置为true,并将其设置为false,然后遇到它。 Eliminate characters in between. 消除两者之间的字符。 Repeat this till your String is parsed. 重复此操作直到解析String。

I think this is the best way, 我认为这是最好的方式,

public static String removeUntil(String str, String c, int st)
{
    StringBuilder sb = new StringBuilder(str);
    str = sb.reverse().toString();
    for(int i = 0;i<st;i++)
        str = str.substring(0,str.lastIndexOf(c));
    sb = new StringBuilder(str);
    str = sb.reverse().toString();
    return str;
}

for ex: 对于前:

removeUntil("I.want.to.remove.this.string.until.third.dot", ".", 3)

then result of it = "remove.this.string.until.third.dot"; 然后它的结果=“remove.this.string.until.third.dot”;

This is a simple implementation for removing specific characters from a string. 这是从字符串中删除特定字符的简单实现。 The time complexity is O(n + m). 时间复杂度为O(n + m)。

Note: This is only for ASCII characters. 注意:这仅适用于ASCII字符。

String s = "Battle 123";
String remove = "e1";
System.out.println(removeCharacters(s,remove));


public static String removeCharacters(String sentence, String remove){
    if(sentence == null) return null;
    if(sentence != null && remove == null) return sentence;
    char[] s = sentence.toCharArray();
    char[] r = remove.toCharArray();
    int dest = 0;
    boolean[] asciiFlags = new boolean[128];        //False

    for(int i = 0;i < r.length; i++){
        asciiFlags[r[i]] = true;
    }

    for(int src =0;src<s.length;src++){
        if(!asciiFlags[s[src]]){
            s[dest++] = s[src];
        }   
    }
    return new String(s,0,dest);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM