I know that this is not what you hoped for, but I would just do something ridiculous:
String str = "foo"; #define MAX_POSSIBLE_LENGTH_OF_STR 16 ... int i[MAX_POSSIBLE_LENGTH_OF_STR];
The idea is that you allocate more space for the array than you really need, and just avoid using extra parts of the array.
Alternatively, if you are not going to change the definition of str in your source code very often, you can save some RAM by doing the following:
String str = "foo"; #define LENGTH_OF_STR 3 ... int i[LENGTH_OF_STR];
source share