如何在java中删除空行和字符的其余部分
我想从字符串中删除空行和其余字符,我想单独解析字符串中的特定值 我想从我的字符串中单独得到这个值23243232,在产品价格之后,我有空行空间,而且我还有一些字符,所以我用那个空行作为分隔符,试图单独得到产品价格。但我也得到了其他值,还有23243232。有人能帮我从这个字符串中得到23243232吗如何在java中删除空行和字符的其余部分,java,string,parsing,Java,String,Parsing,我想从字符串中删除空行和其余字符,我想单独解析字符串中的特定值 我想从我的字符串中单独得到这个值23243232,在产品价格之后,我有空行空间,而且我还有一些字符,所以我用那个空行作为分隔符,试图单独得到产品价格。但我也得到了其他值,还有23243232。有人能帮我从这个字符串中得到23243232吗 String actualResponse = "--sGEFoZV85Qnkco_QAU5b6B3Tt1OrOOFkArwzoF_yDmmW5DfupJDtuHlh20LL2SAbWZb8a3e
String actualResponse = "--sGEFoZV85Qnkco_QAU5b6B3Tt1OrOOFkArwzoF_yDmmW5DfupJDtuHlh20LL2SAbWZb8a3exzoF_yDmmW5DfupJDtuHlh20LL2SAbWZb8a3exsGEFoZV85Qnkco_QAU5b6B3Tt1OrOOFkArw\r\n"
+ "Product-Discription: form-name; productName=\"iPhone\"\r\n" + "Product-Type: Mobile\r\n"
+ "Product-Price: 23243232\r\n" + "\r\n" + "%dsafdfw32.323efaeed\r\n" + "#$#@#@#";
String productPrice = actualResponse.substring(actualResponse.lastIndexOf("Product-Price:") + 15);
System.out.println("Printing product price ..." + productPrice);
String finalString = productPrice.replaceAll(" .*", "");
这是我得到的输出:
Printing product price ...23243232
%dsafdfw32.323efaeed
#$#@#@#
但我只需要23243232—仅此值。这是因为您正在从索引:
actualResponse.lastIndexOf(“产品价格:)+15
到字符串末尾打印整个子字符串
您还需要在substring方法中提供结束索引作为第二个参数
您需要使用以下选项:
int start = actualResponse.lastIndexOf("Product-Price:") + 15;
int end = actualResponse.indexOf("\r\n", start); // The first "\r\n" from the index `start`
String productPrice = actualResponse.substring(start, end);
我想到了第一个解决办法。这不是最好的,但会解决你的问题
StringBuilder finalString =new StringBuilder();
for (Character c : productPrice.toCharArray()) {
if(Character.isDigit(c)){
finalString.append(c);
}else{
break;
}
}
应用正则表达式以获得更大的灵活性
String content = "--sGEFoZV85Qnkco_QAU5b6B3Tt1OrOOFkArwzoF_yDmmW5DfupJDtuHlh20LL2SAbWZb8a3exzoF_yDmmW5DfupJDtuHlh20LL2SAbWZb8a3exsGEFoZV85Qnkco_QAU5b6B3Tt1OrOOFkArw\r\n"
+ "Product-Discription: form-name; productName=\"iPhone\"\r\n" + "Product-Type: Mobile\r\n"
+ "Product-Price: 23243232\r\n" + "\r\n" + "%dsafdfw32.323efaeed\r\n" + "#$#@#@#";
String re1 = "\\bProduct-Price:\\s"; // Word 1
String re2 = "(\\d+)"; // Integer Number 1
Pattern p = Pattern.compile(re1 + re2, Pattern.DOTALL);
Matcher m = p.matcher(content);
while (m.find()) {
for (int i = 0; i <= m.groupCount(); i++) {
System.out.println(String.format("Group=%d | Value=%s",i, m.group(i)));
}
}
String content=“--sGEFoZV85Qnkco_qu5b6b3tt1orofkarzof_yDmmW5DfupJDtuHlh20LL2SAbWZb8a3exzoF_yDmmW5DfupJDtuHlh20LL2SAbWZb8a3exsGEFoZV85Qnkco_qu5b6b3tt1orofkarw\r\n”
+“产品描述:表单名称;产品名称=\”iPhone\“\r\n”+“产品类型:移动\r\n”
+“产品价格:23243232\r\n”+“\r\n”+%dsafdfw32.323efeed\r\n”+“#$#@”;
字符串re1=“\\b产品价格:\\s”//单词1
字符串re2=“(\\d+)”//整数1
Pattern p=Pattern.compile(re1+re2,Pattern.DOTALL);
匹配器m=p.Matcher(内容);
while(m.find()){
对于(int i=0),请考虑提供输出,这是我的输出:打印产品价格…23243232% DSAFDFW32.323。efeed$$但我只想要23243232作为我的输出读取子字符串上的javadocs,提示:尝试使用它的另一个变体不要像对lastIndexOf
的多重求值或对产品价格的长度进行硬编码:
string您可以始终将其保存在一个变量中,然后重新使用它。:)这正是我的意思,实际上我喜欢它,因为它不太依赖硬编码
String content = "--sGEFoZV85Qnkco_QAU5b6B3Tt1OrOOFkArwzoF_yDmmW5DfupJDtuHlh20LL2SAbWZb8a3exzoF_yDmmW5DfupJDtuHlh20LL2SAbWZb8a3exsGEFoZV85Qnkco_QAU5b6B3Tt1OrOOFkArw\r\n"
+ "Product-Discription: form-name; productName=\"iPhone\"\r\n" + "Product-Type: Mobile\r\n"
+ "Product-Price: 23243232\r\n" + "\r\n" + "%dsafdfw32.323efaeed\r\n" + "#$#@#@#";
String re1 = "\\bProduct-Price:\\s"; // Word 1
String re2 = "(\\d+)"; // Integer Number 1
Pattern p = Pattern.compile(re1 + re2, Pattern.DOTALL);
Matcher m = p.matcher(content);
while (m.find()) {
for (int i = 0; i <= m.groupCount(); i++) {
System.out.println(String.format("Group=%d | Value=%s",i, m.group(i)));
}
}