Apex 无法删除字符串中的多个空格
Apex can not remove multiple spaces in String
我正在尝试从以下字符串中删除所有 space:
howabountnow.com,,,itsme.com,,,name,,,hello.com,,,asdasdasdasd.asdasdasda,: :: :;; ;;; :: :: :; ; ;,, ,,, ,,, asdasd.asd,,, ,,,asdsadas,,,dilyan
并且从有多个space的地方只删除一个space,结果是:
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|[198]|DEBUG|domain howabountnow.com,,,itsme.com,,,name,,,hello.com,,,asdasdasdasd.asdasdasda,: :: :;; ;;; :: :: :; ; ;,, ,,, ,,, asdasd.asd,,, ,,,asdsadas,,,dilyan
USER_DEBUG|[203]|DEBUG|spaceInd1: 75
USER_DEBUG|[207]|调试|space工业:-1
USER_DEBUG|[209]|DEBUG|domain1 howabountnow.com,itsme.com,名称,hello.com,asdasdasdasd.asdasdasda,::: :;; ;;;:: :::;;;,asdasd.asd,asdsadas,dilyan
能否请您告知如何从字符串中删除所有 space 或在顶点中用单个 space 替换它们?
此致,
地研
你试过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');
}
}
我正在尝试从以下字符串中删除所有 space:
howabountnow.com,,,itsme.com,,,name,,,hello.com,,,asdasdasdasd.asdasdasda,: :: :;; ;;; :: :: :; ; ;,, ,,, ,,, asdasd.asd,,, ,,,asdsadas,,,dilyan
并且从有多个space的地方只删除一个space,结果是:
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|[198]|DEBUG|domain howabountnow.com,,,itsme.com,,,name,,,hello.com,,,asdasdasdasd.asdasdasda,: :: :;; ;;; :: :: :; ; ;,, ,,, ,,, asdasd.asd,,, ,,,asdsadas,,,dilyan
USER_DEBUG|[203]|DEBUG|spaceInd1: 75 USER_DEBUG|[207]|调试|space工业:-1 USER_DEBUG|[209]|DEBUG|domain1 howabountnow.com,itsme.com,名称,hello.com,asdasdasdasd.asdasdasda,::: :;; ;;;:: :::;;;,asdasd.asd,asdsadas,dilyan
能否请您告知如何从字符串中删除所有 space 或在顶点中用单个 space 替换它们?
此致,
地研
你试过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');
}
}