Jump to page: 1 25  
Page
Thread overview
How to loop through characters of a string in D language?
Dec 08, 2021
BoQsc
Dec 08, 2021
Biotronic
Dec 08, 2021
BoQsc
Dec 09, 2021
kdevel
Dec 10, 2021
forkit
Dec 08, 2021
Adam D Ruppe
Dec 08, 2021
BoQsc
Dec 08, 2021
bauss
Dec 08, 2021
BoQsc
Dec 08, 2021
forkit
Dec 08, 2021
Stanislav Blinov
Dec 08, 2021
forkit
Dec 08, 2021
forkit
Dec 09, 2021
kdevel
Dec 09, 2021
Salih Dincer
Dec 10, 2021
Rumbu
Dec 10, 2021
IGotD-
Dec 10, 2021
Rumbu
Dec 10, 2021
forkit
Dec 10, 2021
Luís Ferreira
Dec 10, 2021
Arjan
Dec 11, 2021
forkit
Dec 11, 2021
forkit
Dec 13, 2021
forkit
Dec 13, 2021
bauss
Dec 11, 2021
Stanislav Blinov
Dec 11, 2021
russhy
Dec 11, 2021
Rumbu
Dec 11, 2021
russhy
Dec 10, 2021
Matheus
Dec 10, 2021
Stanislav Blinov
Dec 10, 2021
Rumbu
Dec 11, 2021
Stanislav Blinov
Dec 23, 2021
Salih Dincer
Dec 23, 2021
Stanislav Blinov
Dec 23, 2021
Salih Dincer
Dec 23, 2021
rumbu
December 08, 2021

Let's say I want to skip characters and build a new string.

The string example to loop/iterate:

import std.stdio;

void main()
{
    string a="abc;def;ab";

}

The character I want to skip: ;

Expected result:

abcdefab
December 08, 2021

On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote:

>

Let's say I want to skip characters and build a new string.

The string example to loop/iterate:

import std.stdio;

void main()
{
    string a="abc;def;ab";

}

The character I want to skip: ;

Expected result:

abcdefab
import std.stdio : writeln;
import std.algorithm.iteration : filter;
import std.conv : to;

void main()
{
    string a = "abc;def;ab";
    string b = a.filter!(c => c != ';').to!string;
    writeln(b);
}
December 08, 2021
On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote:
> The string example to loop/iterate:

foreach(ch; a) {

}

does the individual chars of the string you can also

foreach(dchar ch; a) {

}

to decode the utf 8
December 08, 2021

On Wednesday, 8 December 2021 at 11:35:39 UTC, Biotronic wrote:

>

On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote:

>

Let's say I want to skip characters and build a new string.

The string example to loop/iterate:

import std.stdio;

void main()
{
    string a="abc;def;ab";

}

The character I want to skip: ;

Expected result:

abcdefab

[..]
string b = a.filter!(c => c != ';').to!string;
writeln(b);
}

I somehow have universal cross language hate for this kind of algorithm.
I'm not getting used to the syntax and that leads to poor readability.
But that might be just me.

Anyways,
Here is what I've come up with.

import std.stdio;

void main()
{
    string a = "abc;def;ab";
	string b;
	
	for(int i=0; i<a.length; i++){
		write(i);
		writeln(a[i]);
		if (a[i] != ';'){
			b ~= a[i];
		}
		
	}
	
    writeln(b);
}
December 08, 2021

On Wednesday, 8 December 2021 at 12:49:39 UTC, Adam D Ruppe wrote:

>

On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote:

>

The string example to loop/iterate:

foreach(ch; a) {

}

does the individual chars of the string you can also

foreach(dchar ch; a) {

}

to decode the utf 8

Thanks Adam.

This is how it would look implemented.

import std.stdio;

void main()
{
    string a = "abc;def;ab";
	string b;
	
	foreach(ch; a) {
		if (ch != ';'){
			b ~= ch;
		}
	
		writeln(ch);
	}
	
    writeln(b);
}
December 08, 2021

On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote:

>

Let's say I want to skip characters and build a new string.

The string example to loop/iterate:

import std.stdio;

void main()
{
    string a="abc;def;ab";

}

The character I want to skip: ;

Expected result:

abcdefab

string b = a.replace(";", "");

December 08, 2021

On Wednesday, 8 December 2021 at 14:16:16 UTC, bauss wrote:

>

On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote:

>

Let's say I want to skip characters and build a new string.

The string example to loop/iterate:

import std.stdio;

void main()
{
    string a="abc;def;ab";

}

The character I want to skip: ;

Expected result:

abcdefab

string b = a.replace(";", "");

Thanks, that's what I used to do few years ago.
It's a great solution I forget about and it works.

import std.stdio;
import std.array;

void main()
{
    string a="abc;def;ab";
	string b = a.replace(";", "");
	writeln(b);
}
December 08, 2021

On Wednesday, 8 December 2021 at 14:27:22 UTC, BoQsc wrote:

>

On Wednesday, 8 December 2021 at 14:16:16 UTC, bauss wrote:

>

On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote:

>

Let's say I want to skip characters and build a new string.

The string example to loop/iterate:

import std.stdio;

void main()
{
    string a="abc;def;ab";

}

The character I want to skip: ;

Expected result:

abcdefab

string b = a.replace(";", "");

Thanks, that's what I used to do few years ago.
It's a great solution I forget about and it works.

import std.stdio;
import std.array;

void main()
{
    string a="abc;def;ab";
	string b = a.replace(";", "");
	writeln(b);
}

It's also worth noting the differences in compiler output, as well as the time taken to compile, these two approaches:

(1)
string str = "abc;def;ab".filter!(c => c != ';').to!string;

(2)
string str = "abc;def;ab".replace(";", "");

see: https://d.godbolt.org/z/3dWYsEGsr

December 08, 2021

On Wednesday, 8 December 2021 at 22:18:23 UTC, forkit wrote:

>

It's also worth noting the differences in compiler output, as well as the time taken to compile, these two approaches:

(1)
string str = "abc;def;ab".filter!(c => c != ';').to!string;

(2)
string str = "abc;def;ab".replace(";", "");

see: https://d.godbolt.org/z/3dWYsEGsr

You're passing a literal. Try passing a runtime value (e.g. a command line argument). Also, -O2 -release :) Uless, of course, your goal is to look at debug code.

December 08, 2021

On Wednesday, 8 December 2021 at 22:35:35 UTC, Stanislav Blinov wrote:

>

You're passing a literal. Try passing a runtime value (e.g. a command line argument). Also, -O2 -release :) Uless, of course, your goal is to look at debug code.

but this will change nothing.

the compilation cost of using .replace, will always be apparent (compared to the presented alternative), both in less time taken to compile, and smaller size of executable.

« First   ‹ Prev
1 2 3 4 5