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
Lucy YuanLucy Yuan 

How to judge the String can or can't to convert to integer?

Hi

i just meet one question, when i convert the string to integer type. i used the integer.valueof() method . but if string like 'ABCDEF' that can't  do integer conversion but throw System.TypeException.

could i do judgement first first? like if the string parameter s='123'  can do integer conversion, and then i call the integer.valueof(s)

is there any method ??

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
NaishadhNaishadh

Use below utility class containsonly method for validation

 

 

public class StringUtilities {
	public static Integer indexOfAnyBut(String str, String searchChars) {
		if (isEmpty(str) || searchChars == null) {
		    return -1;
		}
		for (Integer i = 0; i < str.length(); i++) {
		    if(searchChars.contains(charAt(str,i))){
			continue;   
		    }
		    return i;
		}
		return -1;
	}

	public static boolean containsOnly(String str, String valid) {
		if ((valid == null) || (str == null)) {
		    return false;
		}
		if (str.length() == 0) {
		    return true;
		}
		if (valid.length() == 0) {
		    return false;
		}
		return indexOfAnyBut(str, valid) == -1;
	}

	public static String charAt(String str, Integer index) {
		if(str == null){
		    return null;
		}
		if(str.length() <= 0){
		    return str; 
		}
		if(index < 0 || index >= str.length()){
		    return null;    
		}
		return str.substring(index, index+1);
	}

	public static boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}
}

 Example

 

String validChars = '0123456789';
System.debug('IsInteger : ' + StringUtilities.containsOnly('Test',validChars));
System.debug('IsInteger : ' + StringUtilities.containsOnly('12434',validChars));

 

All Answers

gotherthanthougotherthanthou

 

try
{
    integer i = integer.valueOf(s);
    // code to handle converted string here
}
catch(Exception e)
{
    // code to handle strings that can't be converted here
}

 

You may need to adapt the method to your specific needs.

 

Lucy YuanLucy Yuan

Thanks!

But when the program throw exception, the program will stop here to run catch block . i just to convert a list<String> in one loop. if any exception throwing . it jumping out of loop but stop the program. so i want to do the  judement first.

 

 

List<String> stringList = new List<String>();

List<Integer> intList = new List<Integer>();

String s1 = '123';

String s2 = 'ABC';

stringList.add(s2);

stringList.add(s1);

try{

 Interger i ;

 for(String s : stringList){

      i = Integer.valueOf(s);

  }

}catch(Exception ex){

.....

}

 

 

in this scenraio both 2 string will do conversion failure,but what i need is the one of strings that can convert will to convert successfully.

NaishadhNaishadh

Use below utility class containsonly method for validation

 

 

public class StringUtilities {
	public static Integer indexOfAnyBut(String str, String searchChars) {
		if (isEmpty(str) || searchChars == null) {
		    return -1;
		}
		for (Integer i = 0; i < str.length(); i++) {
		    if(searchChars.contains(charAt(str,i))){
			continue;   
		    }
		    return i;
		}
		return -1;
	}

	public static boolean containsOnly(String str, String valid) {
		if ((valid == null) || (str == null)) {
		    return false;
		}
		if (str.length() == 0) {
		    return true;
		}
		if (valid.length() == 0) {
		    return false;
		}
		return indexOfAnyBut(str, valid) == -1;
	}

	public static String charAt(String str, Integer index) {
		if(str == null){
		    return null;
		}
		if(str.length() <= 0){
		    return str; 
		}
		if(index < 0 || index >= str.length()){
		    return null;    
		}
		return str.substring(index, index+1);
	}

	public static boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}
}

 Example

 

String validChars = '0123456789';
System.debug('IsInteger : ' + StringUtilities.containsOnly('Test',validChars));
System.debug('IsInteger : ' + StringUtilities.containsOnly('12434',validChars));

 

This was selected as the best answer
gotherthanthougotherthanthou

 

Why not put the try/catch inside the for-loop?

Lucy Yuan wrote:

Thanks!

But when the program throw exception, the program will stop here to run catch block . i just to convert a list<String> in one loop. if any exception throwing . it jumping out of loop but stop the program. so i want to do the  judement first.

 

 

List<String> stringList = new List<String>();

List<Integer> intList = new List<Integer>();

String s1 = '123';

String s2 = 'ABC';

stringList.add(s2);

stringList.add(s1);

try{

 Interger i ;

 for(String s : stringList){

      i = Integer.valueOf(s);

  }

}catch(Exception ex){

.....

}

 

 

dnakonidnakoni

You're right. Instead of 

 

try {

   Integer i ;

   for (String s : strings) {

          i = Integer.valueOf(s);

    }

}

catch {

}

 

Do this:

 

 

   Integer i ;

   for (String s : strings) {

          try {

                 i = Integer.valueOf(s);

          }

          catch {

                 i = 0; //or whatever default value you need to assign

          }

    }