Java 删除字符串特定位置的字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20354310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:17:54  来源:igfitidea点击:

Remove characters at certain position at string

javaandroidstring

提问by Hassaan Rabbani

I want to remove certain characters at specific positions of the String. I have the positions, but I am facing problems removing the characters.

我想删除字符串特定位置的某些字符。我有这些职位,但我在删除字符时遇到了问题。

what i am doing is:

我正在做的是:

if (string.subSequence(k, k + 4).equals("\n\t\t\t")){
    string = string.subSequence(0, k) + "" + s.subSequence(k, s.length());
}

I need to remove "\n\t\t\t"from string

我需要"\n\t\t\t"从字符串中删除

回答by Ruchira Gayan Ranaweera

Use String.ReplaceAll()instead of this.

String.ReplaceAll()这个代替。

But if you only want to remove specific element only you can use substring(). Now you want to know position which you already know.

但是,如果您只想删除特定元素,则可以使用substring(). 现在你想知道你已经知道的位置。

回答by Zong

Use StringBuilder:

使用StringBuilder

StringBuilder sb = new StringBuilder(str);

sb.delete(start, end);
sb.deleteCharAt(index);

String result = sb.toString();

回答by anguyen

Put your points in a HashSet called set

把你的点放在一个名为的 HashSet 中 set

StringBuilder sb=new StringBuilder();
for(int i=0;i<string.length();i++){
       if(!set.contains(string.charAt(i)))
           sb.append(string.charAt(i));  
 }

 String reformattedString=sb.toString();

回答by Cafebabe

Use StringBuilder

使用 StringBuilder

String str="    ab a acd";
        StringBuilder sb = new StringBuilder(str);

        sb.delete(0,3);
        sb.deleteCharAt(0);

        String result = sb.toString();
        System.out.println(result);

回答by A.Sfon

First you have to put \in front of the special characters in order to do the matching of the two string, thus you will have .equals("\"\\n\\t\\t\\t\""), otherwise the substring is not going to be recognized inside the string. Then the other thing which you have to fix is the position of the index begin and end inside .subSequence(k,k+10)since the first and the last character are 10 positions apart and not 4. Note also that when you patch the string you go from position 0to kand from k+10to str.length(). If you go from 0 --> k and k --> length()you just join the old string together :). Your code should work like this, I have tested it already

首先你必须把\特殊字符放在前面,以便对两个字符串进行匹配,这样你就会有.equals("\"\\n\\t\\t\\t\""),否则字符串内部将无法识别子字符串。然后你必须修复的另一件事是索引开始和结束的位置,.subSequence(k,k+10)因为第一个和最后一个字符相距 10 个位置,而不是 4。还要注意,当你修补字符串时,你从位置0k和从k+10str.length(). 如果你离开,0 --> k and k --> length()你只需将旧字符串连接在一起:)。你的代码应该像这样工作,我已经测试过了

   if(str.substring(k, k+10).equals("\"\n\t\t\t\""))
        {
     newstr = str.substring(0,k)+str.substring(k+10,(str.length()));
         }

also you don't need +" "+since you are adding strings. Whoever wants to see the effect of this can run this simple code:

您也不需要,+" "+因为您正在添加字符串。想看效果的可以运行这个简单的代码:

public class ReplaceChars_20354310_part2 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {


    String str = "This is a weird string containg balndbfhr frfrf br brbfbrf b\"\n\t\t\t\"";

    System.out.println(str); //print str 

    System.out.println(ReplaceChars(str));  //then print after you replace the substring 

    System.out.println("\n");  //skip line 


    String str2 = "Whatever\"\n\t\t\t\"you want to put here"; //print str 

    System.out.println(str2);  //then print after you replace the substring

    System.out.println(ReplaceChars(str2));

}


      //Method ReplaceChars

       public static String ReplaceChars (String str) {

       String newstr ="";

       int k;

        k = str.indexOf("\"\n\t\t\t\""); //position were the string    starts within the larger string

      if(str.substring(k, k+10).equals("\"\n\t\t\t\""))
        {

       newstr = str.substring(0,k)+str.substring(k+10,(str.length())); //or just str

        }


           return newstr;

        }//end method


     }

回答by Machhindra Neupane

public static String remove(int postion, String stringName) {
    char [] charArray = stringName.toCharArray();
    char [] resultArray = new char[charArray.length];
    int count = 0;
    for (int i=0; i< charArray.length; i++) {
        if (i != postion-1) {
            resultArray[count] = charArray[i];
            count++;
        }
    }
    return String.valueOf(resultArray);
}