You need to sign in to do that
Don't have an account?
d3developer
String.split method works only some of the time
This code:
String s = (String) myValueHolder; System.debug('val: ' + s ); if (s != null) { String[] frontBack = s.split('.'); System.debug('FB:' + frontBack.size()); if(frontBack.size() > 1) { System.debug('WOO I made it'); }
Works fine the first time I run it on a page. However, every subsequent time the split returns a list with 0 elements.
For instance, here is a selection from the debug log:
12:53:26.175|USER_DEBUG|[16,5]|DEBUG|val: 920.0 <=== has a period! 12:53:26.175|METHOD_EXIT|[16,5]|debug(ANY) 12:53:26.175|METHOD_ENTRY|[18,18]|String.split(String) 12:53:26.175|METHOD_EXIT|[18,18]|split(String) 12:53:26.175|METHOD_ENTRY|[20,6]|System.debug(String) 12:53:26.175|METHOD_ENTRY|[20,27]|LIST:String.size() 12:53:26.175|METHOD_EXIT|[20,27]|size() 12:53:26.175|USER_DEBUG|[20,6]|DEBUG|FB:0 <=== but zero elements in the resulting list 12:53:26.175|METHOD_EXIT|[20,6]|debug(ANY) 12:53:26.175|METHOD_ENTRY|[21,9]|LIST:String.size()
Any ideas? I've tried saving it to a list, array, initializing the list elsewhere but nothing seems to change the simple fact that the split method doesn't work.
Sorry. I must've copy/pasted the wrong thing at the time. Try this:
Decimal myValueHolder = 920.2;
String s = String.valueOf(myValueHolder);
String[] frontBack = s.split('\\.');
System.debug('FB:' + frontBack);
All Answers
The split function expects a regular expression. The period symbol in a regular expression has special meaning. The period generally matches any character and in this case is not resulting in a split of any kind (because it matches the whole string).
If you are attempting to split your string using a period, you will need to escape it. Here's a brief example:
Decimal myValueHolder = 920.2;
String s = String.valueOf(myValueHolder);
String[] frontBack = s.split('/.');
System.debug('FB:' + frontBack);
Note the forward slash before the period symbol.
For more information on regular expressions, please see the java doc on patterns. It's the method used by Salesforce to process regular expressions.
http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html
Thanks!
Hey, you tricked me!
I finally tried this (instead of my own workaround splitter method) and look what I get.
code:
output:
Sorry. I must've copy/pasted the wrong thing at the time. Try this:
Decimal myValueHolder = 920.2;
String s = String.valueOf(myValueHolder);
String[] frontBack = s.split('\\.');
System.debug('FB:' + frontBack);
This, however, works:
Or, to use your example:
String myVal = 'H.e.ll.o';
String[] frontBack = myVal.split('\\.');
for (String s : frontBack) {
System.debug(s);
}
gives the output:
19.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;VALIDATION,INFO;WORKFLOW,INFO
Execute Anonymous: String myValueHolder = 'H.e.ll.o';
Execute Anonymous: String myVal = 'H.e.ll.o';
Execute Anonymous: String[] frontBack = myVal.split('\\.');
Execute Anonymous:
Execute Anonymous: for (String s : frontBack) {
Execute Anonymous: System.debug(s);
Execute Anonymous: }
18:57:21.590|EXECUTION_STARTED
18:57:21.590|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
18:57:21.598|METHOD_ENTRY|[3,22]|String.split(String)
18:57:21.599|METHOD_EXIT|[3,22]|String.split(String)
18:57:21.599|METHOD_ENTRY|[6,5]|System.debug(String)
18:57:21.599|USER_DEBUG|[6,5]|DEBUG|H
18:57:21.599|METHOD_EXIT|[6,5]|System.debug(String)
18:57:21.599|METHOD_ENTRY|[6,5]|System.debug(String)
18:57:21.599|USER_DEBUG|[6,5]|DEBUG|e
18:57:21.599|METHOD_EXIT|[6,5]|System.debug(String)
18:57:21.599|METHOD_ENTRY|[6,5]|System.debug(String)
18:57:21.599|USER_DEBUG|[6,5]|DEBUG|ll
18:57:21.599|METHOD_EXIT|[6,5]|System.debug(String)
18:57:21.599|METHOD_ENTRY|[6,5]|System.debug(String)
18:57:21.599|USER_DEBUG|[6,5]|DEBUG|o
18:57:21.599|METHOD_EXIT|[6,5]|System.debug(String)
18:57:21.599|CUMULATIVE_LIMIT_USAGE
18:57:21.599|LIMIT_USAGE_FOR_NS|(default)|
np. Thanks again!