java中分割字符串

发布于 2024-11-10 05:24:41 字数 136 浏览 0 评论 0原文

我从程序中获取这个字符串

[user1, user2]

,我需要将其拆分为

String1 = user1
String2 = user2

I am getting this string from a program

[user1, user2]

I need it to be splitted as

String1 = user1
String2 = user2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一梦浮鱼 2024-11-17 05:24:41

您可以执行此操作,以便在以逗号分隔之前安全地删除任何括号或空格:

String input = "[user1, user2]";
String[] strings = input.replaceAll("\\[|\\]| ", "").split(",");
// strings[0] will have "user1"
// strings[1] will have "user2"

You could do this to safely remove any brackets or spaces before splitting on commas:

String input = "[user1, user2]";
String[] strings = input.replaceAll("\\[|\\]| ", "").split(",");
// strings[0] will have "user1"
// strings[1] will have "user2"
只是我以为 2024-11-17 05:24:41

尝试,

            String source = "[user1, user2]";
            String data = source.substring( 1, source.length()-1 );

            String[] split = data.split( "," );

            for( String string : split ) {
                System.out.println(string.trim());
            }

Try,

            String source = "[user1, user2]";
            String data = source.substring( 1, source.length()-1 );

            String[] split = data.split( "," );

            for( String string : split ) {
                System.out.println(string.trim());
            }
神魇的王 2024-11-17 05:24:41

这将完成您的工作,您将收到一个字符串数组。

    String str = "[user1, user2]";
    str = str.substring(1, str.length()-1);
    System.out.println(str);
    String[] str1 = str.split(",");

This will do your job and you will receive an array of string.

    String str = "[user1, user2]";
    str = str.substring(1, str.length()-1);
    System.out.println(str);
    String[] str1 = str.split(",");
迷雾森÷林ヴ 2024-11-17 05:24:41

尝试 String.split() 方法。

Try the String.split() methods.

雪花飘飘的天空 2024-11-17 05:24:41

从您获取此字符串的位置。您可以检查该方法的返回类型吗?

我认为返回类型将是一些数组时间,并且您可以节省 string 中的返回值。所以它附加了[]。如果不是这种情况,您可以使用用户在其他答案中建议的任何方法。

From where you are getting this string.can you check the return type of the method.

i think the return type will be some array time and you are savings that return value in string . so it is appending [ ]. if it is not the case you case use any of the methods the users suggested in other answers.

放我走吧 2024-11-17 05:24:41

从你所说的输入来看,我认为你已经得到了一个数组,不是吗?

String[] users = new String[]{"user1", "user2"};
System.out.println("str="+Arrays.toString(str));//this returns your output

因此,有了这个数组,您就可以使用它们的索引来获取它们。

String user1 = users[0];
String user2 = users[1];

如果您实际上正在使用字符串,则继续,例如@WhiteFang34建议 (+1)。

From the input you are saying I think you are already getting an array, don't you?

String[] users = new String[]{"user1", "user2"};
System.out.println("str="+Arrays.toString(str));//this returns your output

Thus having this array you can get them using their index.

String user1 = users[0];
String user2 = users[1];

If you in fact are working with a String then proceed as, for example, @WhiteFang34 suggests (+1).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文