0

I have a list with datatype "string" But I want to check whether it contains number as a string or not.

1
  • 2
    Do you want to know whether the entire string is a number or just whether parts of the string contain numbers? Does that include decimal numbers or integers only? There's a huge difference in requirements.
    – crmprogdev
    Commented Apr 15, 2016 at 15:30

3 Answers 3

5

Use the isNumeric() method on the strings.

for (String s : strings) {
    if (s.countMatches('.') <= 1) {
        system.debug(s.remove('.').isNumeric());
    } else system.debug(false);
}
2
  • 1
    Cool. Just seems you would have to change the console.log to system.debug() Commented Apr 15, 2016 at 14:15
  • 1
    Lol sorry, too much javascript. Thanks for the edit. Commented Apr 15, 2016 at 14:49
1

Create a Set of Strings which will contain all the elements from the list:

List <String> myList = new List <String> {'1', '2', '3', '2'};
Set <String> mySet = new Set <String> ();
mySet.addAll(myList);

which will remove any duplicates.

Then you can convert your number into a string and check whether the set contains it by using the contains method:

if (mySet.contains(String.valueOf(1))) 
{
    // do something 
}

The other option is to cast the number to a string and then iterate through the List of Strings until you find it:

List <String> myList = new List <String> {'1', '2', '3', '2'};
String myNumberAsString = String.valueOf(3);
Boolean numberFound = false;

for (String listItem : myList)
{
    if (listItem == myNumberAsString)
    {
        numberFound = true;
        break;
    }
}

if (numberFound)
{
    // do something
}
3
  • 1
    Would seem to me that using regEx and/or a PatternMatcher would be simpler than this compared to iterating through a for loop for 0 through 9.
    – crmprogdev
    Commented Apr 15, 2016 at 15:34
  • That's another option :). It all depends on what exactly the requirement is. It's not clear what's in the list and what we're comparing... Commented Apr 16, 2016 at 5:36
  • Agreed, thus the reason for my comment below the OP's post. His question was unclear. Everyone seems to have concluded he was asking about integers and not decimal "numbers" and not clear if he wanted to extract numbers from strings. Knowing all that determines what solution is appropriate. ;)
    – crmprogdev
    Commented Apr 16, 2016 at 13:22
1

You can make use of instanceOf method to compare and find what kind of value the list is storing.

instanceOf - If you need to verify at run time whether an object is actually an instance of a particular class, use the instanceof keyword.

So if you just want to verify if the element within your list of strings is a number(Integer/Decimal) or not, you can make use of instanceOf Integer or instanceOf Decimal

For eg:

List <String> myList = new List <String> {'1.0', 'aajcka', '3.35', '2'};
List<Integer> indexesofElementsAsNumber = new List<Integer>();

for(Integer i=0; i < myList.size(); i++){
    try{
        Object o = Decimal.valueOf(myList[i]);
        Boolean b =  o instanceof Decimal;
        if(b){
              indexesofElementsAsNumber.add(i);
        }
    }
    catch(TypeException te){
         continue;
    }           
}
system.debug('=====List of indexes that contain nummbers===='+indexesofElementsAsNumber);

The above code gives me below output:

=====List of indexes that contain nummbers====(0, 2, 3)

PS: There is a need to make the use of Object class while you do the comparision because if you do Integer.valueOf() instance of Integer will throw error saying Integer is always an instance of Integer.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .