从字符串中删除所有出现的char

本文翻译自:Remove all occurrences of char from string

I can use this: 我可以用这个:

String str = "TextX Xto modifyX";
str = str.replace('X','');//that does not work because there is no such character ''

Is there a way to remove all occurrences of character X from a String in Java? 有没有办法从Java中的字符串中删除所有出现的字符X

I tried this and is not what I want: str.replace('X',' '); //replace with space 我尝试了这个,但不是我想要的: str.replace('X',' '); //replace with space str.replace('X',' '); //replace with space


#1楼

参考:https://stackoom.com/question/JCW8/从字符串中删除所有出现的char


#2楼

You can use str = str.replace("X", ""); 您可以使用str = str.replace("X", ""); as mentioned before and you will be fine. 如前所述,您会没事的。 For your information '' is not an empty (or a valid) character but '\\0' is. 供您参考, ''不是空(​​或有效)字符,而是'\\0'

So you could use str = str.replace('X', '\\0'); 因此,您可以使用str = str.replace('X', '\\0'); instead. 代替。


#3楼

String test = "09-09-2012";
String arr [] = test.split("-");
String ans = "";

for(String t : arr)
    ans+=t;

This is the example for where I have removed the character - from the String. 这是我从字符串中删除字符的示例。


#4楼

Hello Try this code below 您好,请在下面尝试此代码

public class RemoveCharacter {

    public static void main(String[] args){
        String str = "MXy nameX iXs farXazX";
        char x = 'X';
        System.out.println(removeChr(str,x));
    }

    public static String removeChr(String str, char x){
        StringBuilder strBuilder = new StringBuilder();
        char[] rmString = str.toCharArray();
        for(int i=0; i<rmString.length; i++){
            if(rmString[i] == x){

            } else {
                strBuilder.append(rmString[i]);
            }
        }
        return strBuilder.toString();
    }
}

#5楼

I like using RegEx in this occasion: 我喜欢在这种情况下使用RegEx:

str = str.replace(/X/g, '');

where g means global so it will go through your whole string and replace all X with ''; g表示全局,因此它将遍历整个字符串并将所有X替换为''; if you want to replace both X and x, you simply say: 如果要同时替换X和x,则只需说:

str = str.replace(/X|x/g, '');

(see my fiddle here: fiddle ) (在这里查看我的小提琴: 小提琴


#6楼

package com.acn.demo.action;

public class RemoveCharFromString {

    static String input = "";
    public static void main(String[] args) {
        input = "abadbbeb34erterb";
        char token = 'b';
        removeChar(token);
    }

    private static void removeChar(char token) {
        // TODO Auto-generated method stub
        System.out.println(input);
        for (int i=0;i<input.length();i++) {
            if (input.charAt(i) == token) {
            input = input.replace(input.charAt(i), ' ');
                System.out.println("MATCH FOUND");
            }
            input = input.replaceAll(" ", "");
            System.out.println(input);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值