我正在尝试从以下字符串中删除所有空格:
int
并且从有多个空格的地方只删除一个空格,结果是:
howabountnow.com,,,itsme.com,,,name,,,hello.com,,,asdasdasdasd.asdasdasda,: :: :;; ;;; :: :: :; ; ;,, ,,, ,,, asdasd.asd,,, ,,,asdsadas,,,dilyan
以及更多类似的表达方式。
目前我使用以下源代码:
replaceAll('\s+', '');
replaceAll('[ ]+', '');
replace(' ', '');
当我在调试日志中启动代码时,我得到以下内容:
String domain = accountObj.Domain__c;
System.debug('domain ' + domain);
integer spaceInd = domain.indexOf(' ');
System.debug('spaceInd1: ' + spaceInd);
while (spaceInd > -1) {
domain = domain.replace(' ', '');
spaceInd = domain.indexOf(' ');
System.debug('spaceInd: ' + spaceInd);
}
System.debug('domain1 ' + domain);
USER_DEBUG | [203] | DEBUG | spaceInd1:75 USER_DEBUG | [207] | DEBUG | spaceInd:-1 USER_DEBUG | [209] | DEBUG | domain1 howabountnow.com ,,, itsme.com ,,, name ,,, hello.com ,,, asdasdasdasd.asdasdasda,:::: ;; ;;; :: :::;;;;,,,,,,, asdasd.asd ,,,,,, asdsadas ,,, dilyan
请问您如何删除字符串中的所有空格或用顶点中的单个空格替换它们?
此致
递烟
答案 0 :(得分:0)
您是否尝试过deleteWhiteSpace字符串方法?
本单元测试通过我:
@isTest
private class StringTest
{
static testMethod void myStringTest()
{
String test = 'howabountnow.com,,,itsme.com,,,name,,,hello.com,,,asdasdasdasd.asdasdasda,: :: :;; ;;; :: :: :; ; ;,, ,,, ,,, asdasd.asd,,, ,,,asdsadas,,,dilyan';
String noSpaces = test.deleteWhitespace();
System.assertEquals(noSpaces, 'howabountnow.com,,,itsme.com,,,name,,,hello.com,,,asdasdasdasd.asdasdasda,::::;;;;;:::::;;;,,,,,,,,asdasd.asd,,,,,,asdsadas,,,dilyan');
}
}