java - 获取JAVA String[]中的特定元素
问题描述
我希望在 JAVA 的 String[] 中的字符串中获取名为Something的特定元素。
我的代码 =
String sentence = "AP=Something+example|AS=Explanation";
String[] word = sentence.split("\\|");
for (String w: word){
System.out.println(w);
}
我当前的输出:
AP=Something+example
AS=Explanation
我的预期输出应该是:
Something // the other information I dont want to take. Is there a better and faster way and not too time consuming?
先感谢您
解决方案
我不明白你真正想要什么,但是:
String sentence = "AP=Something+example|AS=Explanation";
String[] recs = sentence.split("\\|");
HashMap<String, String> h = new HashMap<String,String>();
for (String r : recs) {
String[] vals = r.split("=");
h.put(vals[0], vals[1]);
}
System.out.println(h.get("AP").split("\\+")[0]);
这段代码太糟糕了,但看看常见的想法。
我的英语也太差了。
推荐阅读
- r - 为什么使用 dummyVars 和 predict 而不是 model.matrix
- vue.js - 如何仅在管理页面('admin/***')上添加 Vuetify?
- php - 是否可以将不同数组中的 3 个相同值合并为一个?
- r - 施加额外的约束会导致 R 中的 hitandrun 包中出现“欠约束问题”错误
- typo3 - 为什么包括 TypoScript 不起作用?
- javascript - 如何将 API url 中的数据转换为 React 中的数组?
- php - php包含带有css样式表的函数outsie根文件夹
- typescript - 为什么我们需要在打字稿中编写函数的泛型?例如函数填充
(){} - html - 如何消除 html/css 中框之间的空格?
- c++ - 如何在 mac OS 11 上创建 GTK+4 应用程序?