使用 android 在字符串中添加双引号

add double quotes in string using android

我在 forloop 中有字符串,我想将其添加到另一个字符串中,如给定格式

for (int i = 0; i < profiles.length(); i++) {
    JSONObject c = profiles.getJSONObject(i);
    String admnno   = c.getString(TAG_ADMNNO);
}

结果应该是这样的

"Rajesh", "Mahesh", "Vijayakumar"
or 
final CharSequence[] items = {"Rajesh", "Mahesh", "Vijayakumar"};

adminno 应该用双引号和逗号括起来。它在 android 中执行 Background()

为此使用\"

例如 String str="\"Rajesh\""

试试这个,

if (TextUtils.isEmpty(string))
        return "";

final int lastPos = string.length() - 1;
if (lastPos < 0 || (string.charAt(0) == '"' && string.charAt(lastPos) == '"'))
        return string;


        return "\"" + string + "\"";

您必须使用 \" 转义引号,如下所示:

public static String getQuotedString(String sample){
      return "\"".concat(sample).concat("\"");
}

你也可以试试这个

var id =  "\"id\": \""

输出=“id”:“

在 Kotlin 中,您可以像这样使用变量值

val searchText = "Android"
textview.text = "5 articles found for \"$searchText\""

对于固定字符串:

String str="5 articles found for \"IOS\" "

价值等于

5 articles found for "IOS"

另一种选择是使用 Kotlin 的多行字符串。当您需要为 UTests 硬编码大 JSON 时特别有用:

val jsonString = """
  {
     "string_key": "value",
     "boolean_key": true
  }
  """