Hot answers tagged

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
30 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
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
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 ...
Raul's user avatar
  • 18.7k
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

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
  • 150k
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
  • 487k
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
  • 92.6k
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....
Christian Carter's user avatar
14 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
  • 150k
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,851
13 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)...
Tanmoy Bhattacharjee's user avatar
13 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'; ...
Ratan Paul's user avatar
  • 22.6k
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
  • 487k
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
  • 150k
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
  • 150k
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.6k
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
  • 487k
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
  • 487k
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
  • 60.9k
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
  • 150k
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
  • 487k
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
  • 487k
9 votes

String Value of ID returns more than ID

Technically, since you're passing the value as a String already, you probably don't need to query the value at all: TM.Meeting__c = ApexPages.currentPage().getParameters().get('EventId'); However, ...
sfdcfox's user avatar
  • 487k
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
  • 487k
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
  • 487k
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

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