我想在java中的单词旁边找到一个单词
我有一些文字我想在java中的单词旁边找到一个单词,java,string,substring,Java,String,Substring,我有一些文字 "Summary : Daily Monthly Yearly" "Amount : $1,401,508,225.38 $34,132,889,672.53 $334,088,690,177.34" 我这里的问题是如何将这些金额存储在各自的字符串中,即每日、每月、每年?根据您的评论,您说您有一个包含三个金额的字符串数组,并且根据您的问题,您希望存储在各自的字符串中。您可以使用for循环并存储在HashMap中 首先按如下方式获取阵列中的时间: String textWith
"Summary : Daily Monthly Yearly"
"Amount : $1,401,508,225.38 $34,132,889,672.53 $334,088,690,177.34"
我这里的问题是如何将这些金额存储在各自的字符串中,即每日、每月、每年?根据您的评论,您说您有一个包含三个金额的字符串数组,并且根据您的问题,您希望存储在各自的字符串中。您可以使用for循环并存储在HashMap中 首先按如下方式获取阵列中的时间:
String textWithTime = "Daily Monthly Yearly";
String[] timearray = textWithTime.split(" ");//note the space inside double quotation
//Make an object of HashMap here
HashMap<String, String> hashMap = new HashMap<String, String>();
for(int i=0;i<stringarray.length();i++){
hashMap.put(timearray[i], amountarray[i]);
}
如果您有一个字符串中的金额,那么还可以执行以下操作
String[] amountarray = textWithAmount.split(" ");//note the space inside double quotation
按以下步骤进行:
String textWithTime = "Daily Monthly Yearly";
String[] timearray = textWithTime.split(" ");//note the space inside double quotation
//Make an object of HashMap here
HashMap<String, String> hashMap = new HashMap<String, String>();
for(int i=0;i<stringarray.length();i++){
hashMap.put(timearray[i], amountarray[i]);
}
要返回键和值,可以执行以下操作:
for (Map.Entry<String, Object> entry : hashMap.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
//Use these key and values
}
如果您的问题是关于如何拆分字符串,请参阅String类的拆分方法。 如果更多的是关于如何构造结果数据,您可能需要执行以下操作: 创建枚举和类:
public enum Period {
DAILY,
MONTHLY,
YEARLY;
}
public class Income {
private Period p;
private double money;
public Income (Period p, double money) {
this.p = p;
this.money = money;
}
....
// getters, etc
}
然后你可以这样使用它:
Income daily = new Income (Period.DAILY, 1401508225.38);
你的问题可能有重复之处,我们无法回答。你说的商店是什么意思?您是否希望以某种方式将它们关联到地图中?还是要连接字符串?是否有摘要和金额的arrray?@Nabin是的,我有字符串[]中的所有三个金额,或者在代码中测试并调试它。问题中提供的示例涉及将字符串拆分为两部分,但这并不意味着它不能处理更多部分,它返回一个数组。另外,我建议你解决我的问题。非常感谢