java里trim(),查询Java中的trim()方法

I asked a question earlier but met harsh criticism, so here I pose it again. Simpler, and rephrased to appeal to those who may have been concerned about the way I asked it before.

BACKGROUND

I am parsing some HTML for information. I have isolated everything in a series of lines but the content I wish to grab and a bunch of spaces after it. To get rid of the spaces, I opted to use trim(), but I have been having trouble. The last few lines of my code are tests:

System.out.println("'" + someString + "'\n'" + someString.trim() + "'");

The results were:

'Sophomore                                          '

'Sophomore                                          '

I was worried I might have a problem with the way I was calling trim(), since we all make mistakes from time to time, so I tested it like this:

String s = " hello ";

System.out.println("'" + s+ "'\n'" + s.trim() + "'");

The results were:

' hello '

'hello'

MY QUESTION

What am I doing wrong? What I want is to get 'Sophomore', not 'Sophomore                                          '

I look forward to your excellent answers (thanks in advance!).

解决方案

String.trim() specifically only removes characters before the first character whose code exceeds \u0020, and after the last such character.

This is insufficient to remove all possible white space characters - Unicode defines several more (with code points above \u0020) that will not be matched by .trim().

Perhaps your white space characters aren't the ones you think they are?

EDIT comments revealed that the extra characters were indeed "special" whitespace characters, specifically \u00a0 which is a Unicode "non-breaking space". To replace those with normal spaces, use:

str = str.replace('\u00a0', ' ');

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值