如何使用 Java 标准 API 从字符串中获取特定数据?

发布于 2024-09-27 14:04:59 字数 224 浏览 6 评论 0原文

我有以下模式:

Jan(COMPANY) &^% Feb(ASP) 567 Mar(INC) 

我希望最终输出为:

String[] one = {"Jan", "Feb", "Mar"};
String[] two = {"COMPANY","ASP","INC"};

请帮忙。有人吗!!

I have the following pattern:

Jan(COMPANY) &^% Feb(ASP) 567 Mar(INC) 

I want the final output to be:

String[] one = {"Jan", "Feb", "Mar"};
String[] two = {"COMPANY","ASP","INC"};

Please help. Anyone!!?

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

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

发布评论

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

评论(2

も让我眼熟你 2024-10-04 14:04:59

将结果存储在 String[] oneString[] Two 中的完整示例如下所示:

import java.util.*;
import java.util.regex.*;

public class Main {
    public static void main(String[] args) {

        String str = "Jan(COMPANY) &^% Feb(ASP) 567 Mar(INC)";

        ArrayList<String> monthArr = new ArrayList<String>();
        ArrayList<String> dataArr  = new ArrayList<String>();

        // Part 1: \\b(\\p{Alpha}+): Word boundary, one or more characters
        // Part 2: \\(([^)]+)\\): "(", anything but ")" one or more times, ")" 
        Pattern p = Pattern.compile("\\b(\\p{Alpha}+)\\(([^)]+)\\)");
        Matcher m = p.matcher(str);

        while (m.find()) {
            monthArr.add(m.group(1));  // m.group(1) = "Jan", "Feb" and so on
            dataArr.add(m.group(2));   // m.group(2) = "COMPANY, "ASP", and so on
        }

        String[] one = monthArr.toArray(new String[0]);
        String[] two = dataArr.toArray(new String[0]);
    }
}

A complete example that stores the results in String[] one and String[] two would look like this:

import java.util.*;
import java.util.regex.*;

public class Main {
    public static void main(String[] args) {

        String str = "Jan(COMPANY) &^% Feb(ASP) 567 Mar(INC)";

        ArrayList<String> monthArr = new ArrayList<String>();
        ArrayList<String> dataArr  = new ArrayList<String>();

        // Part 1: \\b(\\p{Alpha}+): Word boundary, one or more characters
        // Part 2: \\(([^)]+)\\): "(", anything but ")" one or more times, ")" 
        Pattern p = Pattern.compile("\\b(\\p{Alpha}+)\\(([^)]+)\\)");
        Matcher m = p.matcher(str);

        while (m.find()) {
            monthArr.add(m.group(1));  // m.group(1) = "Jan", "Feb" and so on
            dataArr.add(m.group(2));   // m.group(2) = "COMPANY, "ASP", and so on
        }

        String[] one = monthArr.toArray(new String[0]);
        String[] two = dataArr.toArray(new String[0]);
    }
}
慕烟庭风 2024-10-04 14:04:59

遍历字符串中的字符并存储您需要的内容。

String s = "Jan(COMPANY) &^% Feb(ASP) 567 Mar(INC)";

ArrayList<String> monthsList = new ArrayList<String>();
ArrayList<String> companyList = new ArrayList<String>();

StringBuilder b = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);

    switch (c) {

    case ' ':
        b.setLength(0);
        break;
    case '(':
        monthsList.add(b.toString());
        b.setLength(0);
        break;
    case ')':
        companyList.add(b.toString());
        b.setLength(0);
        break;
    default:
        b.append(c);
        break;
    }
}

String[] monthArray = monthsList.toArray(new String[] {});
String[] companyArray = companyList.toArray(new String[] {});

Iterate across the characters in the string and store what you need.

String s = "Jan(COMPANY) &^% Feb(ASP) 567 Mar(INC)";

ArrayList<String> monthsList = new ArrayList<String>();
ArrayList<String> companyList = new ArrayList<String>();

StringBuilder b = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);

    switch (c) {

    case ' ':
        b.setLength(0);
        break;
    case '(':
        monthsList.add(b.toString());
        b.setLength(0);
        break;
    case ')':
        companyList.add(b.toString());
        b.setLength(0);
        break;
    default:
        b.append(c);
        break;
    }
}

String[] monthArray = monthsList.toArray(new String[] {});
String[] companyArray = companyList.toArray(new String[] {});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文