Grasshopper Expression is string an integer

Hi, I’m struggling to find the right format to see if a list of 2 character strings are integers or not. Most are but there are a couple of exceptions. I’m trying to generate a list of True or False.

Any expression I try throws an error when it hits the non-integer entries. Is there a way? I feel I need an ‘IsInt’ function, or something to catch the errors?

(I’d considered forcing it through an Integer data type component, but read that a few key characters can slip through (such as lower case ‘e’))


Is Int Expression.gh (8.0 KB)

Hi @Derek4

This works.

Thanks, that’s almost there, except that it treats the special character ‘e’ as a number - I want it to recognise it as text (so read as null / false)

If you’re open to a Python solution, you could do something like this:

a = x.replace(" ","").isnumeric()

Thanks. Trying to avoid Python if possible (just in case other users have to work with the script) but I’ll keep that as backup!

I guess an alternative approach would be to test for the digits 0 to 9 and the space character - anything else should be discarded (these are room numbers so won’t ever be floating point).

if each of your strings to be filtered contains for sure at least a letter, you can take advantage of the thing that letters can have Upper and Lower case

so you can make a string all uppercase and check if any character changed, or make a whole string lowercase and also check if any character changes

if any of the above happened, then the string contains a letter and goes False

so this answers the True/False statement “this string does not contains letters”, nothing related to integers and symbols


Is Int Expression_Re.gh (8.4 KB)

Thanks, that’s very innovative! Unfortunately they may not all contain a letter - it depends on what our clients write in their spreadsheet! In the test case I’m working with, there are a couple of brackets too.


Using the modulus component, if the remainder is 0, the value is an integer.

Reading the post again, I think regex would be a simple solution.

RegExr: Learn, Build, & Test RegEx

Free Online OCR - convert PDF to Word, Images to text, JPEG to Word

what strings you would like to filter exactly?

symbols are just ignored
this checks just the very last substring separated by a space, like this:


Is Int Expression_Re_re.gh (12.4 KB)

but if you have any particular filtering in mind, it’s usually feasible :+1:

Thanks folks, I’ll give those a try in a bit.

Basically I can’t guarantee what will be in the string, but it may or may not end in a number. If it does, I’d like to separate the number, perform some actions on the rest of the string, and add the number back on if there was one.

Thanks, this seems to do it perfectly - much appreciated :slight_smile:

1 Like