Jump to page: 1 2 3
Thread overview
How to remove all characters from a string, except the integers?
Mar 03, 2022
BoQsc
Mar 03, 2022
Stanislav Blinov
Mar 03, 2022
BoQsc
Mar 03, 2022
bachmeier
Mar 03, 2022
Salih Dincer
Mar 03, 2022
matheus
Mar 03, 2022
forkit
Mar 03, 2022
H. S. Teoh
Mar 03, 2022
matheus
Mar 03, 2022
H. S. Teoh
Mar 04, 2022
matheus
Mar 04, 2022
H. S. Teoh
Mar 04, 2022
matheus
Mar 04, 2022
ag0aep6g
Mar 04, 2022
H. S. Teoh
Mar 04, 2022
matheus
Mar 04, 2022
Stanislav Blinov
Mar 04, 2022
matheus
Mar 04, 2022
Ali Çehreli
Mar 04, 2022
Salih Dincer
Mar 04, 2022
Salih Dincer
Mar 04, 2022
H. S. Teoh
Mar 04, 2022
Salih Dincer
Mar 04, 2022
forkit
Mar 04, 2022
Salih Dincer
Mar 04, 2022
Ali Çehreli
Mar 04, 2022
BoQsc
Mar 04, 2022
Ali Çehreli
Mar 04, 2022
Salih Dincer
March 03, 2022

I need to check if a string contains integers,
and if it contains integers, remove all the regular string characters.
I've looked around and it seems using regex is the only closest solution.

import std.stdio;

void main(string[] args){

	if (args.length > 1){
	
		write(args[1]); // Needs to print only integers.
		
	} else {
		write("Please write an argument.");
	}
}
March 03, 2022

On Thursday, 3 March 2022 at 12:14:13 UTC, BoQsc wrote:

>

I need to check if a string contains integers,
and if it contains integers, remove all the regular string characters.
I've looked around and it seems using regex is the only closest solution.

import std.stdio;
import std.algorithm : find, filter;
import std.conv : to;
import std.uni : isNumber;

void main(string[] args){
    if (args.length > 1){
	
        auto filtered = () {
            auto r = args[1].find!isNumber; // check if a string contains integers
            return r.length ?
                   r.filter!isNumber.to!string // and if it does, keep only integers
                   : args[1];                  // otherwise keep original
        } ();

        filtered.writeln;
		
    } else {
        write("Please write an argument.");
    }
}
March 03, 2022

On Thursday, 3 March 2022 at 13:25:32 UTC, Stanislav Blinov wrote:

>

On Thursday, 3 March 2022 at 12:14:13 UTC, BoQsc wrote:

>

I need to check if a string contains integers,
and if it contains integers, remove all the regular string characters.
I've looked around and it seems using regex is the only closest solution.

import std.stdio;
import std.algorithm : find, filter;
import std.conv : to;
import std.uni : isNumber;

void main(string[] args){
    if (args.length > 1){
	
        auto filtered = () {
            auto r = args[1].find!isNumber; // check if a string contains integers
            return r.length ?
                   r.filter!isNumber.to!string // and if it does, keep only integers
                   : args[1];                  // otherwise keep original
        } ();

        filtered.writeln;
		
    } else {
        write("Please write an argument.");
    }
}

D language should be renamed into Exclamation-mark language.
It feels overused everywhere and without a better alternative.

March 03, 2022

On Thursday, 3 March 2022 at 13:25:32 UTC, Stanislav Blinov wrote:

>
    auto filtered = () {
        auto r = args[1].find!isNumber; // check if a string contains integers

When using find!isNumber:

0123456789
@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
`abcdefghijklmnopqrstuvwxyz{|}~

When using find!isAlphaNum:

0123456789
March 03, 2022

On Thursday, 3 March 2022 at 13:55:47 UTC, BoQsc wrote:

>

On Thursday, 3 March 2022 at 13:25:32 UTC, Stanislav Blinov wrote:

>

On Thursday, 3 March 2022 at 12:14:13 UTC, BoQsc wrote:

>

I need to check if a string contains integers,
and if it contains integers, remove all the regular string characters.
I've looked around and it seems using regex is the only closest solution.

import std.stdio;
import std.algorithm : find, filter;
import std.conv : to;
import std.uni : isNumber;

void main(string[] args){
    if (args.length > 1){
	
        auto filtered = () {
            auto r = args[1].find!isNumber; // check if a string contains integers
            return r.length ?
                   r.filter!isNumber.to!string // and if it does, keep only integers
                   : args[1];                  // otherwise keep original
        } ();

        filtered.writeln;
		
    } else {
        write("Please write an argument.");
    }
}

D language should be renamed into Exclamation-mark language.
It feels overused everywhere and without a better alternative.

But you have no problem with parenthesis and braces?

March 03, 2022
On Thursday, 3 March 2022 at 12:14:13 UTC, BoQsc wrote:
> I've looked around and it seems using regex is the only closest solution.

I'm a simple man who uses D with the old C mentality:

import std.stdio;

void main(){
    string s, str = "4A0B1de!2C9~6";
    foreach(i;str){
        if(i < '0' || i > '9'){ continue; }
        s ~= i;
    }
    writeln("Result: ", s);
}

Result: 401296

Matheus.
March 03, 2022
On Thursday, 3 March 2022 at 19:28:36 UTC, matheus wrote:
>
> I'm a simple man who uses D with the old C mentality:
>
> import std.stdio;
>
> void main(){
>     string s, str = "4A0B1de!2C9~6";
>     foreach(i;str){
>         if(i < '0' || i > '9'){ continue; }
>         s ~= i;
>     }
>     writeln("Result: ", s);
> }
>
> Result: 401296
>
> Matheus.

mmm..but we no longer live in simple times ;-)

(i.e. unicode)
March 03, 2022
On Thu, Mar 03, 2022 at 08:23:14PM +0000, forkit via Digitalmars-d-learn wrote:
> On Thursday, 3 March 2022 at 19:28:36 UTC, matheus wrote:
> > 
> > I'm a simple man who uses D with the old C mentality:
> > 
> > import std.stdio;
> > 
> > void main(){
> >     string s, str = "4A0B1de!2C9~6";
> >     foreach(i;str){
> >         if(i < '0' || i > '9'){ continue; }
> >         s ~= i;
> >     }
> >     writeln("Result: ", s);
> > }
> > 
> > Result: 401296
> > 
> > Matheus.
> 
> mmm..but we no longer live in simple times ;-)
> 
> (i.e. unicode)

------
void main() {
	string s = "blahblah123blehbleh456bluhbluh";
	auto result = s.filter!(ch => ch.isDigit).to!int;
	assert(result == 123456);
}
------

Problem solved.  Why write 6 lines when 3 will do?


T

-- 
People tell me that I'm skeptical, but I don't believe them.
March 03, 2022
On Thursday, 3 March 2022 at 21:03:40 UTC, H. S. Teoh wrote:
> ...
>
> ------
> void main() {
> 	string s = "blahblah123blehbleh456bluhbluh";
> 	auto result = s.filter!(ch => ch.isDigit).to!int;
> 	assert(result == 123456);
> }
> ------
>
> Problem solved.  Why write 6 lines when 3 will do?

Just because I'm a simple man. :)

I usually program mostly in C and when in D, I go in the same way but using features like: GC, strings, AA etc.

Of course your version is a D'ish way of handling things, and I can't contest it looks better visually. But if size was problem I could have written:

void main(){
    string s, str = "4A0B1de!2C9~6";
    foreach(i;str){
    	(i >= '0' && i <= '9') ? s~=i : null;
    }
    writeln(s);
}

Well still 1 line off, but I goes with my flow. I mean this example is a simple one, but usually I can see and understand what a code in C is doing (more) easily than D just looking at it. Don't even ask about C++, because I gave up. :)

Matheus.

PS: I spotted something on your code, you're converting the result to int, this can lead to a overflow depending the values in the string.
March 03, 2022
On Thu, Mar 03, 2022 at 10:54:39PM +0000, matheus via Digitalmars-d-learn wrote:
> On Thursday, 3 March 2022 at 21:03:40 UTC, H. S. Teoh wrote:
[...]
> > ------
> > void main() {
> > 	string s = "blahblah123blehbleh456bluhbluh";
> > 	auto result = s.filter!(ch => ch.isDigit).to!int;
> > 	assert(result == 123456);
> > }
> > ------
[...]
> PS: I spotted something on your code, you're converting the result to int, this can lead to a overflow depending the values in the string.

If you need to, convert to long instead.

Or if you want a string for subsequent manipulation, replace `int` with `string`.

Or, if you don't actually need to manipulate the value at all, but just print the digits, then it becomes even simpler:

	void main() {
		string s = "blahblah123blehbleh456bluhbluh";
		writeln(s.filter!(ch => ch.isDigit));
	}

This version doesn't even allocate extra storage for the filtered digits, since no storage is actually needed (each digit is spooled directly to the output).


T

-- 
The peace of mind---from knowing that viruses which exploit Microsoft system vulnerabilities cannot touch Linux---is priceless. -- Frustrated system administrator.
« First   ‹ Prev
1 2 3