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

Salih Dincer salihdb at hotmail.com
Fri Mar 4 03:31:01 UTC 2022


On Friday, 4 March 2022 at 02:36:35 UTC, Ali Çehreli wrote:
> On 3/3/22 13:03, H. S. Teoh wrote:
>
> > 	string s = "blahblah123blehbleh456bluhbluh";
>
> > 	assert(result == 123456);
>
> I assumed it would generate separate integers 123 and 456. I 
> started to implement a range with findSkip, findSplit, and 
> friends but failed. :/
>
> Ali

It's called hit two targets with one arrow:

```d
auto splitNumbers(string str)
{
   size_t[] n = [0];
   size_t i;
   foreach(s; str)
   {
     if(s >= '0' && s <= '9')
     {
       n[i] = 10 * n[i] + (s - '0');
     } else {
       i++;
       n.length++;
     }
   }
   return n.filter!(c => c > 0);
}

void main()
{
   auto s = "abc1234567890def1234567890xyz";
   s.splitNumbers.writeln; // [1234567890, 1234567890]
}
```
SDB at 79


More information about the Digitalmars-d-learn mailing list