SoFunction
Updated on 2025-03-08

Java String string content implementation add double quotes

Use escape character \ (backslash)

String str1 = "I am"China\"worker";
String str2 = "\"I am a Chinese worker\"";
(str1);//Output I am a "Chinese" worker
(str2);//Output "I am a Chinese worker"

Supplementary knowledge:Java-Splicing problem with "" double quote marks when outputting character variables

I won't say much nonsense, let's just read the code~

public class Demo {
     public static void main(String[] args) {
          /**
           * The initial value of a character variable is a character
           *
           * in conclusion:
           *
           * When there is no splicing "" in the output statement, the character variable is output whatever is assigned;
           * When there are 2 or more character variables before "", it is converted to int type for calculation and output;
           * When there are 1 or 0 character variables before "", the entire output statement is converted into a string type and output;
           */
          char ch1 = 'A';
          char ch2 = 'B';
          // output whatever the initial value is          (ch1);            // A
          // Convert to int type first and perform calculations          (ch1+ ch2);         // 131
          // Convert to int type first and perform calculations          (ch1+ ch2 + "");      // 131
          // ""Double quotes are converted to int type first, and the calculation is performed, ""Double quotes are converted to string type          (ch1+ ch2 + "" + 'C');   // 131C
          // ""Double quotes are converted into strings before and after          (ch1 + "" + ch2 + 'C');   // ABC
          // ""Double quotes are converted into strings before and after          (ch1 + "" + ch2);      // AB
          // ""Double quotes are converted into strings          ("" + ch1+ ch2);      // AB
          /**
           * The initial value of a character variable is a character
           *
           * in conclusion:
           *
           * When there is no splicing "" in the output statement, the character variable is output whatever is assigned;
           * When there are 2 or more character variables before "", it is converted to int type for calculation and output;
           * When there are 1 or 0 character variables before "", the entire output statement is converted into a string type and output;
           */
          ch1 = 65;
          ch2 = 66;
          char ch3 = 67;
          // output whatever is initial value          (ch1);           // 65
          // ""Double quotes are converted to int type first, and then output after calculation          (ch1 + ch2 + "");     // 131
          // ""Double quotes are converted to character type output          ( ch1 + "");        // A
          // ""Double quotes are converted to int type first, and the calculation is performed, ""Double quotes are converted to string type          (ch1 + ch2 + "" + ch3);  // 131C
          // ""Double quotes are converted into strings before and after          (ch3 + "" + ch1 + ch2);  // CAB     
				  /**
           * When the splicing object is a string, the result and the initial value of the character variable are the same as the result.
           *
           * in conclusion:
           *
           * When there is no splicing "" in the output statement, the character variable is output whatever is assigned;
           * When there are 2 or more character variables before "", it is converted to int type for calculation and output;
           * When there are 1 or 0 character variables before "", the entire output statement is converted into a string type and output;
           */
          int a = 1, b = 2 ;
      	  String c = "3";
       	  (a + b + c );       // 33
          (c + a + b);       // 312
     }
}
   

The above Java String string content implementation adds double quotes is all the content I share with you. I hope you can give you a reference and I hope you support me more.