Hitesh Sheth
Hitesh Sheth

Reputation: 53

How to replace special character In Android?

I have to create file with user define name. If User can use the special character then i want to replace that special character with my specific string. i found the method like this.

String replaceString(String string) {
   return string.replaceAll("special_char","");
}

but how to use this method.?

Upvotes: 2

Views: 8520

Answers (4)

Caner Yılmaz
Caner Yılmaz

Reputation: 199

**this method make two line your string data after specific word **

public static String makeTwoPart(String data, String cutAfterThisWord){
    String result = "";

    String val1 = data.substring(0, data.indexOf(cutAfterThisWord));

    String va12 = data.substring(val1.length(), data.length());

    String secondWord = va12.replace(cutAfterThisWord, "");

    Log.d("VAL_2", secondWord);

    String firstWord = data.replace(secondWord, "");

    Log.d("VAL_1", firstWord);

    result = firstWord + "\n" + secondWord;


    return result;
}

Upvotes: 0

user4571931
user4571931

Reputation:

use below function to replace your string

 public static String getString(String p_value)
 {

        String p_value1 = p_value;
        if(p_value1 != null && ! p_value1.isEmpty())
        {
            p_value1 = p_value1.replace("Replace string from", "Replace String to");
            return p_value1;
        }
        else
        {
            return "";
        }
 }

example

Replace string from = "\n"; Replace String to = "\r\n";

after using above function \n is replace with \r\n

Upvotes: 0

Rustam
Rustam

Reputation: 6515

Try

regular expression

   static String replaceString(String string) {
       return string.replaceAll("[^A-Za-z0-9 ]","");// removing all special character.
    }

call

 public static void main(String[] args) {

     String str=replaceString("Hello\t\t\t.. how\t\t are\t you."); // call to replace special character.
     System.out.println(str);
  }

output:

Hello how are you

Upvotes: 1

Bhavesh Rangani
Bhavesh Rangani

Reputation: 1510

relpaceAll method is required regular expression and replace string.

string.replaceAll("regularExpression","replaceString");

You can use this regular expression :

"[;\\/:*?\"<>|&']"

e.g.

String replaceString(String string) {
   return string.replaceAll("[;\\/:*?\"<>|&']","replaceString");
}

Upvotes: 4

Related Questions