简体   繁体   English

Java删除字符串上的空格

[英]Java remove whitespace on string

I crawl a site and i get some prices from it. 我搜寻一个网站,并从中得到一些价格。 I get a price with its currency ( 21,00 TL ) i should remove currency(TL) and the left whitespace on it for convert double to string. 我得到了其货币的价格( 21,00 TL ),我应该删除currency(TL)并留在其上的空白,以便将double转换为string。 In short i should get 21.00 . 简而言之,我应该得到21.00。 Whatevery i did , i couldnt remove that whitespace. 我所做的一切,我都无法删除该空白。

I got from crawler : 我来自履带:

<b>21,00&nbsp;TL</b>

What i try: 我尝试的是:

price_lower_str = price_lower_str.replace("&nbsp;TL","");

and 

price_lower_str = price_lower_str.replace(" TL","");

price_lower_str = price_lower_str.replace("TL","");
price_lower_str = price_lower_trim();

but i couldnt get only 21.00 . 但我只能得到21.00。 Who can help me? 谁能帮我?

Thanks 谢谢

Quick and dirty, but working :-) 快速又肮脏,但是可以工作:-)

public static void main(String[] args) {
    String str = "<b>21,00&nbsp;TL</b>";
    Matcher matcher = Pattern.compile(".*?([\\d]+,[\\d]+).*").matcher(str);
    if (matcher.matches()) System.out.println(matcher.group(1).replace(',', '.'));
}

OUTPUT: 输出:

21.00

You're just using the wrong regular expression. 您只是使用了错误的正则表达式。 Try this: 尝试这个:

price_lower_str.replaceAll("(\\&nbsp;|\\s)+TL", "")

First, I'm using replaceAll and not just replace as you are. 首先,我正在使用replaceAll,而不仅仅是按原样替换。 Second, notice the parens - I'm replacing EITHER &nbsp; 其次,请注意括号-我要替换EITHER&nbsp; OR \\s which matches any whitespace character. 或\\ s匹配任何空白字符。 Finally, I'm escaping via backslashes the ampersand in &nbsp; 最后,我在&nbsp;中通过反斜杠“&”号进行转义。 Escaping backslashes when backslash itself is a meta-character in regex is a pain, but welcome to java regex. 当反斜杠本身是regex中的元字符时,转义反斜杠是很痛苦的,但是欢迎使用Java regex。

Using regexes sound to heavy for this simple processing. 对于这种简单处理,使用正则表达式听起来很沉重。 It's not really efficient in that case. 在这种情况下,效率不是很高。 What you could do is to locate the > from the < b > tag and do a substring up to the amperstand. 您可以做的是找到<b>标记中的>,并做一个子字符串直到安培架。

System.out.println(test.substring(test.indexOf(">")+1, test.indexOf("&"))); System.out.println(test.substring(test.indexOf(“>”)+ 1,test.indexOf(“&”))));;

You will get your answer 21,00 您会得到答案21,00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM