9 votes
Accepted

Extract all occurences of a substring from a string

This is a simple "regular expression" situation. You can use the following code as a starting point: Pattern p = Pattern.compile('\\{!.+?\\}'); string templateContent = 'hello adrian , this is for {!...
  • 445k
6 votes
Accepted

System.StringException: Ending position out of bounds:

The abbreviate method is a good choice: CusObj2.ProdName__c = CusObj2.Name__c.abbrevate(99); as it: Returns an abbreviated version of the String, of the specified length and with ellipses ...
  • 131k
5 votes
Accepted

AMPScript SubString Adding in Additional Character

substring takes a length argument as the third option, and using the index value from the space gives 1 more length than the first name. Either skip adding 1 to the @WhiteSpacePos value, or use @...
5 votes

Replace double character with single in string

You can use the replaceAll() method from the String class. String testString = 'Hello;; my name is SF support.'; testString = testString.replaceAll(';;', ','); System.debug(testString); Result: ...
  • 4,819
5 votes
Accepted

substring assignment to limit data to custom field

Instead of tuncating your self why not let salesforce truncate it for you using dml options? string getBodyString = response.getBody(); logRecord.Status_Code__c = response.getStatusCode(); ...
4 votes
Accepted

How do I check if substring is out of bounds?

This is a classic bounds-overflow bug. Before you take a fixed-length substring or request a specific string index, you must check whether the string contains enough characters to respond to the ...
  • 88.3k
4 votes
Accepted

Check If String Starts With Any Of A Set<String>?

First thing that comes to my mind is to use the String substringBefore(separator) method. String s1 = 'abuse@localhost'; String s2 = s1.substringBefore('@'); // s2 will contain 'abuse' From there, ...
  • 50.7k
4 votes
Accepted

Can you combine substring and indexOfChar in the same line?

indexOfChar() needs to operate on an instance of a string (it's not a static method) e.g. String myStr = 'test'; system.debug(myStr.indexOfChar('s')); Trying to get at your value by substrings is a ...
  • 50.7k
4 votes
Accepted

How do I remove repeating parts?

We usually move the reusable piece of code to a common utility method. You can create a private method that will return the sanitized comment back. private static String getSanitizedComment(String ...
3 votes
Accepted

Ending position out of bounds: 10

Use left instead: z.Patient_Id_Help__c=z.Patient_Name__c.left(10); It does not throw an exception if there is less than 10 characters. Remember to check for null first, if applicable: if(z....
  • 445k
3 votes
Accepted

How to fetch the values from the String to store in a seprate string upto certain part

Your String is in fact valid JSON. Therefore, you can parse it into Map and get values by key. Map<String, Object> ans = (Map<String, Object>) JSON.deserializeUntyped(getResult); Integer ...
3 votes

Check If String Starts With Any Of A Set<String>?

I would use regular expressions for this problem. I know, I know, now you have two problems. Anyway: public static Boolean startsWithAny(String input, Set<String> substrings) { if (input == ...
  • 144k
3 votes
Accepted

Check For Special Characters in Emails

Use String.containsNone() to check whether a string contains any prohibited characters, or String.containsOnly() to check that it only contains a specific set of characters. That said, be aware that ...
  • 88.3k
3 votes
Accepted

What does the .substring(3) function do?

From the String class documentation substring(startIndex) Returns a new String that begins with the character at the specified zero-based startIndex and extends to the end of the String. So given ...
  • 50.7k
2 votes
Accepted

Can I use Ampscript to remove the last character from a URL?

You can do this using the Substring(1,2,3), Length(1) and Subtract(1,2) functions. It is pretty easy and you need to supply the original string, the index you want to start with (1) and the length ...
2 votes
Accepted

trim namefield from right in SOQL

Might this work for you? SELECT Name, Body, BodyLength FROM Attachment WHERE ParentId IN :ids AND (NOT Name LIKE '%log.pdf') I'm assuming ids is a list or a set and not a single id? If it is a ...
  • 2,753
2 votes

Using IF Substring to change link or image based on the market the email goes to

You're missing some substring() functions. I'd also recommend using the AttributeValue() function to retrieve the value for ISO_CNTRY_2_CD. %%[ var @isoCntry2Cd set @isoCntry2Cd = AttributeValue(...
  • 28.3k
2 votes
Accepted

Capture everything but the last 3 digits

Very easily. YOu get the length of the string and then use substring based on length minues your number: %%[ SET @num = "17,873" SET @length = Length(@num) SET @final = SubString(@num, 1, Subtract(@...
  • 28.7k
2 votes
Accepted

Error on Apex Trigger when trying to use "substringAfter(' ')"

I see you've used my example to attempt to bulkify the trigger - good work! You have a bug though that you'll need to fix (that will fix your null error and make it all work properly) Change the ...
2 votes

Error on Apex Trigger when trying to use "substringAfter(' ')"

Most probably, staffMap does not contain a key of a.Referral_Store__c.substringAfter(' ') You're populating staffMap with a key of staffMember.Name. Is that going to have the same value as a....
  • 5,574
2 votes
Accepted

Is it possible to remove of filter keysets by a standard method and only maintain keyset which start with a string

I don't know of anything out-of-the-box Apex that will do that kind of filtering. A sample helper method in Apex would look like this: public Set<String> filterStartsWith(Set<String> ...
  • 4,782
2 votes

How Can I Get A Substring From A Long Text Area Field?

You cannot reference Long Text Area fields in formulas of any type. You will have to use Apex. For example, if you have a controller already, set a truncatedDescription instead of just Description. ...
  • 144k
2 votes

Replace character in string in specific position (ie not first, not all)

Apex doesn't really support that. Strings are as in Java, immutable objects, so you can only create modified copies, but not update the original. It has to do with Java's String Pool. So, replaceFirst ...
  • 445k
2 votes
Accepted

Salesforce regular expression get id value from string from prefix and Name of the record

When you use parentheses in a regex, you create a "capture group". These groups are numbered from 1 to x, where x is the number of capture groups you create this way. Group "0" is ...
  • 445k
2 votes
Accepted

Substring Ampscript extracting url within quotes

This is kind of hacky, but should work if the input matches the syntax of your example: SET @emailLinklong= [Request__c:Site_URL__c] SET @rowSet = BuildRowsetFromString(@emailLinklong, "”") ...
2 votes
Accepted

Modify then concatenate String

It seems to me that you already have code that parses the lines in the email body: the emailSearch() method. This is already being used by earlier lines in the main code body. Examples: newLead....
  • 2,336
1 vote
Accepted

lightning controller get string value

Those are not method of String object in Javascript. They are mothods of Apex String class. You could use substring and indexOf. const recUrl = 'CF00N4A00000CMi5V=04612373&CF00N4A00000CMi5V_lkid=...
  • 4,371
1 vote

Substring in LWC Javascript

This is more of a JS question and not specific to LWC. Considering your all other input to the function is correct, you have a case sensitivity issue here on indexOf function as currently written. The ...
  • 29.4k
1 vote

Can you combine substring and indexOfChar in the same line?

if you always expect that same estructure you can use response.getbody().substringbetween('t/','"'); Either using substringAfterLast('/').substringBefore('"'); would work too. Take a look to the ...
  • 11
1 vote

Replace substring with masking character in Visualforce

Would something like this suffice? I’ll leave it up to you to define what happens if the part before the @ is 2 or less characters. String valueToMask = original.substring(2, original.indexOf('@')); ...
  • 13.3k

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