Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.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_Android_String - Fatal编程技术网

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

Java 删除字符串中特定位置的字符,java,android,string,Java,Android,String,我想删除字符串特定位置的某些字符。我有位置,但我面临着删除角色的问题 我正在做的是: if (string.subSequence(k, k + 4).equals("\n\t\t\t")){ string = string.subSequence(0, k) + "" + s.subSequence(k, s.length()); } 我需要从字符串中删除“\n\t\t\t”使用string.ReplaceAll() 但是,如果您只想删除特定的元素,您可以使用substring()。

我想删除字符串特定位置的某些字符。我有位置,但我面临着删除角色的问题

我正在做的是:

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

我需要从字符串中删除
“\n\t\t\t”
使用
string.ReplaceAll()

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

使用:


将您的点放入名为
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();
StringBuilder sb=新建StringBuilder();
对于(int i=0;i使用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);

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

   if(str.substring(k, k+10).equals("\"\\n\\t\\t\\t\""))
        {
     newstr = str.substring(0,k)+str.substring(k+10,(str.length()));
         }
另外,您不需要
+“”+
,因为您正在添加字符串。任何想看到这一效果的人都可以运行以下简单代码:

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


     }
publicstaticstringremove(int position,stringstringname){
char[]charArray=stringName.toCharArray();
char[]resultArray=新字符[charArray.length];
整数计数=0;
for(int i=0;i
改用
String.replaceAll
吗?我不想替换所有出现的内容,我想替换某些位置的一些内容。请使用
子字符串(索引)
查找您的位置。将字符串转换为字符数组。将该索引处的字符更改为新字符。转换回字符串。看看这个解决方案我不想替换所有出现的情况,我想在某些位置替换其中一些。请详细说明您的答案,我的技术有什么问题?@HassaanRabbani我在您的实现中没有看到任何字符串被替换或删除。虽然这段代码可以回答这个问题,提供关于如何和/或为什么解决问题的附加上下文将提高答案的长期价值。
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


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