function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ChubbyChubby 

Split the comma separated values and check length of each value

Hi All,

I have a requirement where the field holds comma separated values. Let's say 1234,4567,7890 like this. I want to split these values and check each value length. If length is <4 then error message should be displayed to user. Please help me how to achive this. I know that i should use split method first but after that how to check each field length?

Your inputs appreciated.

Thanks.
Best Answer chosen by Chubby
Abdul KhatriAbdul Khatri
You can create a method in a class passing the string. Here is the code for you. You can change the classname or add the method in any of your existing class
 
public class StringFunctions {

    Public class wrongLengthException extends Exception {}

    public static  void StringLengthCheck(String strValue) {
    
    String[] strArray = strValue.Split(',');
    
        for(String str : strArray) {
            
             If (str.length() < 4)
                  Throw new wrongLengthException ( 'Incorrect length value found');
        }
    }
}

You can use the method as follows
try {
    StringFunctions.StringLengthCheck('1234,5678,912');
} catch (Exception ex) {
    system.debug(ex.getMessage());
}

All Answers

Abdul KhatriAbdul Khatri
Public class wrongLengthException extends Exception {}

String strValue = ‘1234,3456,334’;
String[] strArray = sreValue.Split(‘,’);

for(String str : strArray) {
    
     If (str.length() < 4)
          Throw new wrongLengthException ( ‘Incorrect length value found’);
}

I hope this help
Abdul KhatriAbdul Khatri
Was that helpful?

Please correct the single quote. I guess since I did that solution from my phone it put the wrong one. Anyways you sill automatcially see that in the code editor.

Please do provide your feedback.
ChubbyChubby
Hi Abdul,

Thanks for your prompt reply. let me try this nad get back.

Thanks.
Abdul KhatriAbdul Khatri
Any update?
ChubbyChubby
Hi Abdul,

Yes this helps alot. But i need to get the field value dynamically instead of directly passing to a string variable.

Thanks.
ChubbyChubby
Hi All,

Could someone pleasehelp me further on this. I need to display error message if user enters field value as 1234,5678,912 and trying to save record. Logic should check each value before comma and check legth of it. I need this to be executed in before insert and update.

Thanks.
GhanshyamChoudhariGhanshyamChoudhari
String strValue = '1234,5678,912';
String[] strArray = strValue.Split(',');

for(String str : strArray) {
if(str=='1234'){
System.debug('error');
}
else if(str=='5678'){
System.debug('error');
}
else if(str=='912'){
System.debug('error');
}else {
System.debug('NO error');
}
}

 
Abdul KhatriAbdul Khatri
You can create a method in a class passing the string. Here is the code for you. You can change the classname or add the method in any of your existing class
 
public class StringFunctions {

    Public class wrongLengthException extends Exception {}

    public static  void StringLengthCheck(String strValue) {
    
    String[] strArray = strValue.Split(',');
    
        for(String str : strArray) {
            
             If (str.length() < 4)
                  Throw new wrongLengthException ( 'Incorrect length value found');
        }
    }
}

You can use the method as follows
try {
    StringFunctions.StringLengthCheck('1234,5678,912');
} catch (Exception ex) {
    system.debug(ex.getMessage());
}
This was selected as the best answer
ChubbyChubby
Hi Abdul,

Thank you so much. It helps me alot.

Thanks.