How to remove all characters from a string, except the integers?

Salih Dincer salihdb at hotmail.com
Fri Mar 4 04:59:09 UTC 2022


On Friday, 4 March 2022 at 02:36:35 UTC, Ali Çehreli wrote:
> 
> I assumed it would generate separate integers 123 and 456. I 
> started to implement a range with findSkip, findSplit, and 
> friends but failed. :/

I worked on it a little.  I guess it's better that way.  But I 
didn't think about negative numbers.
```d
auto splitNumbers(string str) {
   size_t[] n;
   int i = -1;
   bool nextNumber = true;

   foreach(s; str)
   {
     if(s >= '0' && s <= '9')
     {
       if(nextNumber)
       {
         i++;
         n.length++;
         nextNumber = false;
       }
       n[i] = 10 * n[i] + (s - '0');
     }
     else nextNumber = true;
   }
   return n;

} unittest {

   auto n = splitNumbers(" 1,23, 456\n\r7890...");
   assert(n[0] == 1Lu);
   assert(n[1] == 23Lu);
   assert(n[2] == 456Lu);
   assert(n[3] == 7890Lu);
}
```
Presumably, D has more active and short possibilities. This is 
what I can do that making little use of the library.

Thank you...

SDB at 79



More information about the Digitalmars-d-learn mailing list