Android replaces double quotes with one quote

String output "data", "data1", "data2", "data3"

I want it to replace " with ' , so the output of the string will be 'data', 'data1', 'data2', 'data3'

Thank:)

-5
java android string replace
Aug 12 '14 at 7:12
source share
3 answers

use class method stringAll

Syntax: public String replaceAll (String regularExpression, String replacement)

In your case

 String str = "data", "data1", "data2", "data3"; str = str.replaceAll("\"", "'"); System.out.println(str); 

Then you get the output as

 'data','data1','data2','data3' 

From Android API replaceAll says

Matches the regular expression inside this string with the specified replacement.

If the same regular expression is to be used for multiple operations, it may be more efficient to reuse the compiled template.

+4
Aug 12 '14 at 7:16
source share

just use the String class to make this happen as:

 String s = "data"; String replace = s.replace( "\"", "'"); System.out.println(replace); 
+2
Aug 12 '14 at 7:21
source share



Method 1: Using the replaceALL

  String myInput = "\"data1\",\"data2\",\"data3\",\"data4\",\"data5\""; String myOutput = myInput.replaceAll("\"", "'"); System.out.println("My Output with Single Quotes is : " +myOutput); 

Output:

 My Output with Single Quotes is : 'data1','data2','data3','data4','data5' 



Method 2 : use Pattern.compile

  import java.util.regex.Pattern; String myInput = "\"data1\",\"data2\",\"data3\",\"data4\",\"data5\""; String myOutputWithRegEX = Pattern.compile("\"").matcher(myInput).replaceAll("'"); System.out.println("My Output with Single Quotes is : " +myOutputWithRegEX); 



Method 3 : Using Apache Commons , as defined in the link below:

 http://commons.apache.org/proper/commons-lang/javadocs/api-z.1/org/apache/commons/lang3/StringUtils.html#replace(java.lang.String, java.lang.String, java.lang.String) 



0
Aug 12 '14 at 7:18
source share



All Articles