Hot answers tagged

74 votes
Accepted

How to check in Apex if a Text field is blank

I use the isBlank(String) method on the string class in order to check whether a string is either null, white space or empty. String.isBlank(record.txt_Field__c); The documentation says: Returns ...
user avatar
  • 2,182
31 votes
Accepted

Split string Apex

Use String.split(regExp, limit) method: Documentation says Returns a list that contains each substring of the String that is terminated by either the regular expression regExp or the end of the ...
user avatar
25 votes

How to truncate a string to a specific length if it is longer?

As an indicator to the user that the string has been shortened, also adding '...' to the end can be helpful. There is a String method that does that: String newString = sizeString.abbreviate(120); ...
user avatar
  • 129k
20 votes
Accepted

Catch Regex too complicated when splitting a string in salesforce

Not a catch per say (IIRC you cannot catch it, especially if Catch Exception did not work) but will solve the problem of regex to complex, use a custom iterator: CLASS public with sharing class ...
user avatar
  • 53.1k
18 votes
Accepted

Remove whitespace before and after the string dynamically?

use trim() method of String class Returns a copy of the string that no longer contains any leading or trailing white space characters. String s1 = ' Hello! '; String trimmed = s1.trim(); ...
user avatar
18 votes

Remove whitespace before and after the string dynamically?

I like using normalizeSpace() instead of trim() for such scenarios, as it also handles repeated white space. normalizeSpace() Returns the current String with leading, trailing, and repeating white ...
user avatar
  • 17.5k
16 votes

Split string Apex

One option is to use the substringBefore and substringAfter methods. String delimiter = '-'; String input = 'this-is-test-data'; String firstSplit = input.substringBefore(delimiter); // 'this' String ...
user avatar
  • 142k
16 votes
Accepted

Which is Better/Faster isEmpty() or isBlank()?

String.isEmpty is marginally faster (~0.00219ms vs ~0.00206ms, about 6%). This is such a trivially small amount that there's no reason to worry about which one you use from a performance perspective. ...
user avatar
  • 429k
16 votes
Accepted

Is Apex Sometimes Case Sensitive?

Yes. Collection membership is case sensitive. This applies to Sets: If the set contains String elements, the elements are case-sensitive. Two set elements that differ only by case are considered ...
user avatar
  • 85.9k
15 votes
Accepted

Eliminating Unicode accents in Apex

This is a newer revision of Adrian Larson's previous answer for benchmark comparison. This should work quite well for short-ish text fields quite well at the expense of some bytes of heap. For a ...
user avatar
14 votes
Accepted

String Value of ID returns more than ID

You're calling String.valueOf() on the Event Sobject itself, not the Id string. As written, your first line should be: EventIdString = String.valueOf( [select id from Event where Id=: (ApexPages....
user avatar
14 votes

Get Visualforce page Name in Apex controller

How about: String pageName = ApexPages.currentPage().getUrl().substringBetween('apex/', '?'); or, if you do not have parameters: String pageName = ApexPages.currentPage().getUrl().substringAfter('...
user avatar
14 votes
Accepted

How to check empty value in a string?

Use String.isBlank(yourString) to check if it's null or '' or ' '. This method detects whether string has null or blank space or just quotes. Example: String str = 'searchValue'; System.assertEquals(...
user avatar
  • 3,778
13 votes
Accepted

Is there a simple way to convert any value direct to String in production code?

The String class has several static utility methods to transform a value into its string representation, like this: String.valueOf(variableName); That should solve your problem.
user avatar
  • 40.3k
13 votes
Accepted

String.valueOf strange behaviour

I'd expect this code to crash, because ActivityDate can be null, which means that tsk.ActivityDate.format() should throw a NullPointerException. I can demonstrate this through the following code: ...
user avatar
  • 429k
12 votes

How to check in Apex if a Text field is blank

The difference is slight between isEmpty() and isBlank() isBlank(inputString): Returns true if the specified String is white space, empty (''), or null; otherwise, returns false. isEmpty(inputString)...
user avatar
12 votes

Function to convert String to Decimal value?

IsNumeric() method works for only Integer value not for decimal value like this String str = '10'; system.debug('===strToDec=='+str.isNumeric()); This will return true. String str = '10.25'; ...
user avatar
12 votes
Accepted

String to enum value

Update The Winter 22 release is adding Enum.valueOf support. public Enum Season {WINTER, SPRING, SUMMER, FALL} String currentSeasonInput = 'winter'; Season currentSeason = Season.valueOf(...
user avatar
  • 142k
12 votes

Eliminating Unicode accents in Apex

Someone has already published a complete solution. You just need to translate this answer from Javascript to Apex. The author of that answer credits this post, which was posted by lehel on May 6, 2011 ...
user avatar
  • 142k
12 votes
Accepted

Replace Line Breaks (Multiple count as 1) in a string

This is a good time for replaceAll(regex, replacement): String testString = 'Line 1\r\nLine 2\r\n\r\nLine 3'; system.debug(testString.replaceAll('(\\r|\\n)+', ' ** ')); Results: Line 1 ** Line 2 *...
user avatar
  • 142k
11 votes
Accepted

problem replacing and splitting string

Split in Apex is very similar to Java string split. the pattern you pass to the split method is not a direct match, its a REGEX pattern. so the pipe symbol | is considered as a Regex input and it ...
user avatar
11 votes
Accepted

String.format escape curly braces

This is how this is done System.debug(LoggingLevel.INFO,String.format('\'{\'{0}\'}\'', new String[] { String.valueOf(777) })); produces {777} SFDC String.format (also used by VF apex:...
user avatar
  • 61.4k
11 votes
Accepted

Apex code String to char toCharArray

You can use the String.split() method, using '' (two single quotes) as a parameter:: String ourString = 'Salesforce SE'; String[] stringCharacters = ourString.split(''); System.debug(stringCharacters)...
user avatar
11 votes
Accepted

How to check if String is Decimal?

I suppose you could make your own isNumeric() method to also cover decimal values: public class StringUtilities{ public static Boolean isNumeric(String s){ Boolean ReturnValue; ...
user avatar
  • 12.2k
10 votes
Accepted

String method startsWith(String) is case-sensitive or not?

The String.startsWith method is case sensitive. However there is a String.startsWithIgnoreCase method which as the name notes is case insensitive.
user avatar
  • 22.1k
10 votes

Regex Help- Escaping Characters And Matcher Method

The simplest solution is to use the \W character as your entire expression, it matches any non-word character. Unfortunately, this solution would also match any whitespace characters, and ignore the ...
user avatar
  • 142k
10 votes
Accepted

Regex Help- Escaping Characters And Matcher Method

You can match all punctuation using \\p{Punct}, as mentioned in the Pattern class, which matches: !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ For example, the following code results in an empty ...
user avatar
  • 429k
10 votes
Accepted

Why does NULL count as 4 characters in Apex?

In Apex Code, null actually doesn't count as any bytes (per se). However, when you add null to a string, it's typed coerced into a string four-characters long-- "null". This behavior seems to be ...
user avatar
  • 429k
9 votes
Accepted

How to remove ( from the string

Something like : String s = '(09890)'; Integer i = Integer.valueOf(s.replaceAll('[\\(\\)]',''));
user avatar
  • 5,478
9 votes
Accepted

How to replace 'MyFirstName' by 'My First Name'

I will answer to my own question : String text = 'ILikeSalesforcePlatform'; text = String.valueOf(text.splitByCharacterTypeCamelCase()).replaceAll('[\\,\\(\\)]',''); System.debug(text); Output : ...
user avatar
  • 5,478

Only top scored, non community-wiki answers of a minimum length are eligible