Questions tagged [string]

In computer programming, a string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. In Apex it is considered as any set of characters surrounded by single quotes.

Filter by
Sorted by
Tagged with
31
votes
4answers
65k views

How to check in Apex if a Text field is blank

I'm unsure what's the shortest and most robust way to check whether a Text field is blank / empty? /*1*/ Boolean isBlank = record.txt_Field__c == ''; /*2*/ Boolean isBlank = record.txt_Field__c == ...
30
votes
1answer
17k views

Bug in String.split('.')?

I can hardly believe it but split() doesn't work on the . character: String createDate = '01.2.2013'; String[] cd = createDate.split('.'); System.assertEquals(3, cd.size()); // Boom! Tried with '\....
19
votes
2answers
20k views

String length exceeds maximum: 6000000

Are there any work arounds for this issue? I'm trying to upload a file(attachment) to an 3rd party REST API Service. It works fine until my file sizes get about ~5 mb. Then I start getting the ...
12
votes
1answer
21k views

RestRequest RequestBody instead of Params

I am leveraging a webhook from a third party API (Mandrill) that sends me JSON, and have set up a Force.com public site with a RestResource to handle the incoming request. I've been noticing that if ...
11
votes
2answers
7k views

How do I do a case-insensitive replace all for a string?

I've got a project where I'd like to replicate some of the internal merge field functionality so users can build a link and it'd be really helpful if I could do a case-insensitive string replaceAll. ...
10
votes
2answers
17k views

Split string Apex

Need to split a string by first occurence of specific character like '-' in 'this-is-test-data'. so what i want is when i do split it will return 'this' in zero index and rest in first index.
10
votes
3answers
3k views

Eliminating Unicode accents in Apex

Is there a published way to take text that may have accent marks and remove the accent marks? For example: System.assertEquals('Hello', transform('Ḧéļḻṏ')); I believe there is an equivalent method ...
8
votes
4answers
1k views

String Value of ID returns more than ID

I have a visualforce page that I pass a related Event's id to the url to reference. I want to then push it into a field. the resulting UR of the page is for example: /apex/NewTripMeetingEntry?...
8
votes
1answer
4k views

Catch Regex too complicated when splitting a string in salesforce

I have created a function wherein I split a string from the csv filecsvAsString.split('\n') . It is working when the string is not so big. But when I try to split a very large string, an error will ...
8
votes
1answer
14k views

How can I call a method from visualforce?

You would think this would be more obvious, but "how can I call a method from a visualforce page?" Say I have something like this: <apex:page controller="PreChatController "> <script> ...
8
votes
1answer
3k views

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

Given a string with the following variations where {br} is a line break not a literal Line 1{br}Line 2 Line 1{br}{br}Line2 Line 1{br}{br}{br}Line 2{br} Basically any number of line breaks between ...
8
votes
3answers
40k views

JSON and escaped double quote

I'm currently making a callout to a 3rd party vendor and I'm getting a huge response. What is currently messing me up is this section: "MessageSeq":13,"Description":"config file: config-qa1.txt\"","...
8
votes
2answers
5k views

String.format escape curly braces

Is there a way to escape curly braces within String.format? E.g. if I want to get the following output: {777} I'd expected {{ would do the job but this code throws an exception: String.format('...
8
votes
2answers
1k views

replace characters while keeping original parts

Is there an easy and elegant way to add characters in a String at specified places around found characters? The way I started thinking was like this: String regExp = '[\\d]'; String toFormat = '1 ...
8
votes
3answers
24k views

How to convert a number into a formatted string?

Note: there is an extensive discussion on converting a string into a decimal. This question asks how to convert a number into a formatted string. Given a decimal variable, how can I convert that ...
7
votes
2answers
15k views

Coverting a String to Array of Characters

I would like to ask a simple question but seems to be complex if we talk in context of Apex programming. Like in java we have a simple method to get the array of characters by using public char[] ...
7
votes
3answers
1k views

System.StringException thrown when aborting a job in a rest controller

This code works in an Execute Anonymous window, but not when run from a rest controller: @RestResource(urlMapping='/scheduler') global with sharing class SchedulerController { @...
7
votes
2answers
6k views

Set default value for method inputs

How do you set a default value for a method input? public void myMethod(String input1, String input2); I would like to allow people to only need to state one of the inputs if they want (and have ...
7
votes
3answers
4k views

Unable to use multiline with pattern

I'm trying to match a string with Pattern/Matcher, however it is failing because I cannot set Pattern.MULTILINE like the Java documentation uses. String testString = 'Line 1: Does not match\nCustomer ...
7
votes
1answer
650 views

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

Is there any easy way, by using regex and replaceAll method, to tranform MyFirstName by My First Name. I know I could loop through each character but I was wondering if it was possible to do that in ...
7
votes
1answer
5k views

Lower case SOQL with 'IN' operator

I am trying to make a SOQL query trying to match email ids with lower case: SELECT Id, Custom_Email_Id from Custom_Object where Custom_Email_Id IN :list_of_email_ids Here, I am trying to get all ...
7
votes
2answers
557 views

Case sensitive highlighting of keywords

we have a rich text field in one of our objects that contains an html-document. I have created a search page and a user can click on a 'preview'-button which opens the html in a lightbox. I want to ...
6
votes
5answers
2k views

Is Apex Sometimes Case Sensitive?

I have a string set of field names populated as below: Set<String> setAllCustomSettingFields = new Set<String>(); Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); ...
6
votes
3answers
20k views

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

How can I make it that I can reduce the length of a String? Like for example the string.Length() = 300. My goal is to make it to 120 characters, retaining the left side (e.g. NameOfThePerson to ...
6
votes
5answers
8k views

Get Visualforce page Name in Apex controller

I want get current page name of visulforce page. I have tried this and it works for the page having no query parameters in the URL but it fails in the pages having one or more parameters. Any ...
6
votes
3answers
4k views

Substring Error - Ending Position Out Of Bounds

I am trying to copy a Rich Text Field (32k) into multiple Rich Text Field (2k). I get this error: System.StringException: Ending position out of bounds:2000. Code trigger UpdateCaseResponse on ...
6
votes
3answers
8k views

How to check if String is Decimal?

Seems like String.IsNumeric() returns TRUE if it's integer. String numString = '12'; System.Debug(numString.IsNumeric()); If it's a decimal (eg. 12.2) this is returning FALSE. I believe it should ...
6
votes
2answers
10k views

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

When I do a System.debug(whatever), SFDC doesn't care whether whatever is a String, a Date, a Datetime, a Boolean, etc... it just makes a string of it and dumps it in the log. But in the following ...
6
votes
3answers
10k views

How to split a string by the newline character in Apex?

I wonder if it's possible to use the apex public String[] split(String regExp) method to split a string by the newline characters. The string being a csv file created on the Windows platform. I ...
6
votes
3answers
34k views

Function to convert String to Decimal value?

I need to convert String into Decimal. the isNumeric() method is retuning false if value is Decimal. Another alternate: try { Integer x = Integer.valueOf(myString); } catch (exception e) { // ...
6
votes
2answers
5k views

String to enum value [duplicate]

How can I convert String to enum value? public enum Season {WINTER, SPRING, SUMMER, FALL} Season season1; String winterString = 'WINTER'; season1 = winterString; <-- This gives error of course, ...
6
votes
2answers
2k views

Sanitize a string before injecting it into SOSL

Is there a way through the .net framework or some other package to sanitize strings so that they can be safely used in sosl? Is there any larger standard that SOSL falls under? I was able to find How ...
6
votes
1answer
85 views

Detect whether a string contains characters that will not be saved into sObject text field

As a simple example, consider the following string, where I can assign it to an sObject field and it retains the original value, but after inserting the sObject, the value returned when querying the ...
5
votes
2answers
4k views

Regex Help- Escaping Characters And Matcher Method

I have a string: allPunctuation = '~!@#$%^*()_+|}{":?><`=;/.,][-\'\\'; This string contains APEX's way of escaping characters so I can include the ' and the \ characters. When I pass this ...
5
votes
3answers
3k views

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

There is a method that has some conditional logic and returns an empty String '' (no space) in some cases. Which method is more effective/faster to use in this case: String.isEmpty() or String.isBlank(...
5
votes
2answers
9k views

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

When i tried to run the following code snippet anonymously in dev console i got different boolean results. Is string method startsWith(String) case-sensitive or not? String s1 = 'Thomas Inc'; System....
5
votes
2answers
283 views

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

During a conference call with a collegue, I saw him using list of Strings while using Ids, referencing to a list of Ids ; I asked him why this choice, he answered 'Ids are strings so it's about the ...
5
votes
2answers
18k views

Include single quotation mark in string

I'm trying to add a single quote to the following code if(txo.Target__c != null){ targetsToCreate.add(txo); }else{ o.addError(' Please ...
5
votes
1answer
3k views

Storing hash values (blob) in a string field?

I could sit and mess around with this but I really don't know the finer points of how cryptography works in Java/Apex to risk sitting on a security hole. Basically I'm creating a hash and then ...
5
votes
3answers
1k views

JSON Parser getCurrentToken()

I have a JSON string that I want to parse out. I have use the below code many times but for whatever reason this time it is not working. Can anyone poke any holes in this logic? Or point out why this ...
5
votes
1answer
4k views

Apex code String to char toCharArray

In Apex code how do we convert string to array. Java toCharArray does not work. String string = 'somestring' to [s, o, m, e ...]
5
votes
1answer
360 views

Attempt To dereference null object for != in if and no error for == in if

I have been trying to use this code: system.debug('aaaa'+o.Registration_No__c); if(o.Registration_No__c !=null || o.Registration_No__c.trim() !='') { bFirst.RegisterNo = o.Registration_No__c ; }...
5
votes
1answer
7k views

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

So I have a bit of code that is supposed to pad strings of with zeros if they have a length of less than six in order to get them up to six. To accomplish this I've used the String.leftPad(Integer, ...
5
votes
1answer
592 views

Please explain the replaceall method in the code below and how the output is not replace by $1

String x = 'The !shorn! sheep !sprang!.'; System.debug (x.replaceAll ('!(.*?)!', '$1')); The output is: The shorn sheep sprang.
5
votes
3answers
2k views

Email Parsing with apex class

Anybody having knowledge regarding the Email Parsing?? I have a requirement that i receive data from email and i need to split it. for ex: *Category : (Some Data) *Type : (Some Data) I need to ...
5
votes
1answer
173 views

Phone to String Formula Field

Suppose I have Contact Record with FirstName, LastName and Phone, where in phone i am inserting my Mobile no. e.g. FirstName: ABC LastName : XYZ Phone : 9921125797 MyFormulaField ...
5
votes
1answer
7k views

Example of using String.format() with a number / does this function even work?

Per the docs format(String, List<String>) Treat the current string as a pattern that should be used for substitution in the same manner as apex:outputText. Which would lead me to think ...
5
votes
1answer
759 views

string list in custom controller not populating

I have an apex code for a visualforce page but I'm having a bit of an issue returning values from the account object which is one of the master objects in my junction object. The code below shows the ...
5
votes
1answer
2k views

I need a String method in a Trigger to validate if a Rich Text Area is blank

I need assistance in evaluating whether a Rich Text Area custom field is blank using an Apex trigger. Rich Text Field name: Application_Workaround__c I have tried the following within the context ...
4
votes
3answers
4k views

Formatting Negative Numbers with Parentheses

Formatting numbers on a VF page, but I do a bunch of processing the Apex Class, so I would rather return the formatted String value from the controller. I am doing something like this, but I just ...

1
2 3 4 5
8