Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中内置字符串函数的字符串转换_Java_String_Data Structures - Fatal编程技术网

java中内置字符串函数的字符串转换

java中内置字符串函数的字符串转换,java,string,data-structures,Java,String,Data Structures,如何转换如下所示的字符串 String str="[in,us,eu,af,th]"; 进入 只需使用字符串函数: str = str.substring(1,str.length()-2); // remove brackets String a[] = str.split(","); //split it. publicstaticvoidmain(字符串[]args){ 字符串str=“[in,us,eu,af,th]”; str=str.substring(1,str.length(

如何转换如下所示的字符串

String str="[in,us,eu,af,th]";
进入


只需使用字符串函数:

str = str.substring(1,str.length()-2); // remove brackets
String a[] = str.split(","); //split it.
publicstaticvoidmain(字符串[]args){
字符串str=“[in,us,eu,af,th]”;
str=str.substring(1,str.length()-1);
//#1拆下起始和结束支架
System.out.println(“不带括号的字符串:”+str);
//#2以逗号分隔
String[]stringTokens=str.split(“,”);
StringBuffer outputStrBuffer=新的StringBuffer();
outputStrBuffer.append(“[”;//追加起始方括号

对于(int i=0;我尝试
str.replaceAll(“[\\[\\]]”,”).split(“,”)
您可以通过多种方式进行此操作。继续。@Thomas我尝试过!!但它给出了错误,您链接的内容不是我建议的,也没有错误提示…以上代码的输出:字符串前缀和后缀,用双引号分隔:[“in”,“us”,“eu”,“af”,“th”]您可以将其添加到您的答案中,而不是添加注释,还可以添加一些关于代码如何工作以及为什么这样做的描述。
str = str.substring(1,str.length()-2); // remove brackets
String a[] = str.split(","); //split it.
public static void main(String[] args) {
    String str = "[in,us,eu,af,th]";

    str = str.substring(1, str.length() - 1);

    // #1 remove starting and ending brackets
    System.out.println("String without brackets : " + str);

    //#2 split by comma
    String[] stringTokens = str.split(",");
    StringBuffer outputStrBuffer = new StringBuffer();

    outputStrBuffer.append("["); // append starting square bracket

    for (int i = 0; i <= stringTokens.length - 1; i++) {
        //prefix and postfix each token with double quote
        outputStrBuffer.append("\"");
        outputStrBuffer.append(stringTokens[i]);
        outputStrBuffer.append("\"");

        // for last toke dont append comma at end
        if (i != stringTokens.length - 1) {
            outputStrBuffer.append(",");
        }
    }
    outputStrBuffer.append("]"); // append ending square bracket

    System.out.println("String prefixed & postfixed with double quotes, separated by comma : " + outputStrBuffer.toString());
}
String result = str.replace("[", "[\"").replace(",", "\",\"").replace("]", "\"]");