Skip to main content
33 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); ...
Keith C's user avatar
  • 136k
32 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 ...
Oleksandr Berehovskyi's user avatar
20 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(); ...
Oleksandr Berehovskyi's user avatar
19 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 ...
Raul's user avatar
  • 18.8k
17 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 ...
Adrian Larson's user avatar
  • 151k
16 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 ...
Charles Koppelman's user avatar
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. ...
sfdcfox's user avatar
  • 497k
15 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(...
Adrian Larson's user avatar
  • 151k
15 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 ...
David Reed's user avatar
  • 93.2k
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('...
Phil Hawthorn's 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(...
Rohit Mourya's user avatar
  • 3,871
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: ...
sfdcfox's user avatar
  • 497k
13 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 ...
Adrian Larson's user avatar
  • 151k
13 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 *...
Adrian Larson's user avatar
  • 151k
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; ...
martin's user avatar
  • 12.7k
11 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 ...
sfdcfox's user avatar
  • 497k
11 votes
Accepted

Using single quote by curly braces in String.format in Dynamic SOQL

Quoting the merge field makes it literal text. In order to get the merge field to work, you have to quote the quotes: String query = String.format('SELECT Id FROM Availability WHERE Person__c = \'\'{...
sfdcfox's user avatar
  • 497k
11 votes
Accepted

Invalid conversion from runtime type String to Double

If you are going to cast a variable, you’re most likely doing what’s known as a downcast. This means that you’re taking the Object and casting it into a more “specific” type of Object. Here’s an ...
Oleksandr Berehovskyi's user avatar
10 votes
Accepted

String.leftPad(Integer, String) method not working as expected

Looks like an undocumented feature to me. Worked up a quick test in one of my dev environments, and I got it to both compile and work. Strangely enough, it even works when I turn down the Salesforce ...
Derek F's user avatar
  • 62.8k
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 ...
Adrian Larson's user avatar
  • 151k
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 ...
sfdcfox's user avatar
  • 497k
10 votes
Accepted

How to check if a string contains an element from a list

A shorter version of your code just checks the domain directly: Boolean found = domainNames.contains(email.split('@',2)[1]); There's other ways to do this, too, but this is probably the most ...
sfdcfox's user avatar
  • 497k
9 votes
Accepted

Substring Error - Ending Position Out Of Bounds

As an alternative to using substring, you can also use Pattern/Matcher: SObjectField[] fields = new SObjectField[] { // List your fields... Case.Response_Part1__c, Case.Response_Part2__c, ...
sfdcfox's user avatar
  • 497k
9 votes
Accepted

break line on a String

The following should allow you to make your query easier to read. String query = 'SELECT Project__c,' + ' Project__r.Testing_Submitted__c,' + ' Project__r....
TSmith's user avatar
  • 5,052
9 votes
Accepted

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

Use substring method of String class Returns a new String that begins with the character at the specified zero-based startIndex and extends to the character at endIndex - 1. String sizeString = '...
Oleksandr Berehovskyi's user avatar
9 votes
Accepted

lightning component empty field check?

Use empty to determine if something is empty (or, in this case, not empty): {!not(empty(v.guid))} However, I'm not sure you can do all this with the "default" attribute, since it's not meant to be ...
sfdcfox's user avatar
  • 497k
9 votes
Accepted

How to choose using Collection<Id> rather than Collection<String>, or the opposite?

Both String and Id are primitive data types in apex. Consider below scenario: public class poc { static List<Id> ids = new List<Id>(); public static void check() { try { ...
salesforce-sas's user avatar
9 votes
Accepted

Extra Comma at the end of the string

For such purposes I recommend you to use String.join() method, which joins the elements of the specified iterable object, such as a List, into a single String separated by the specified separator: ...
Oleh Berehovskyi's user avatar
8 votes

Substring Error - Ending Position Out Of Bounds

Note that you do not need to skip an index between substrings. Your current pattern actually loses data. When you do substring(2001, 4000), you start at character #2002. When you do substring(0, 2000)...
Adrian Larson's user avatar
  • 151k
8 votes
Accepted

Get Visualforce page Name in Apex controller

Another solution Use $CurrentPage.Name here in VF page use <input type="hidden" name="currentvfpage" value="{!$CurrentPage.Name}"/> And in controller get the hidden input value String ...
Ratan Paul's user avatar
  • 22.7k

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