I have a custom label ('hcv') which has values ICT;Thérapie;

I have a Text field in Vf page in which user enters a value and save it. Before saving, i need to check if the entered value is same as in label. else, it should show warning message.

If i use snippet,System.label.hcv.contains('test') it works fine and shows error.

but for System.label.hcv.contains('CT') // not ICT as in label it returns true (should return false for my case) because of 'contains'.

Is there a way to check for exact value.? my clients dont wanna use picklist value for this.

  • 1
    Your "answer" should have been a comment (I may never have seen it if not for the fact this window was still open), but using Set/contains works, but it's inflexible. I find that the "extra" effort of using a regular expression almost always pays off in the long run. Use whichever you'd like, but keep in mind that the Set method is far less powerful.
    – sfdcfox
    Dec 4, 2017 at 12:20

1 Answer 1


You should use a Pattern (by which, of course, I'm referring to a "regular expression"). First, change your label to be a regular expression:


Where () simply creates a "group", and | means "OR" (as you might expect). You can use as many OR values as you'd like in a row.

Then use the Pattern and Matcher classes:

Pattern p = Pattern.compile(Label.hct);
Matcher m = p.matcher('test');
if(!m.matches()) { // The text does not match
    // show an error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.