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
Thread overview | |||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 08, 2021 How to loop through characters of a string in D language? | ||||
---|---|---|---|---|
| ||||
Let's say I want to skip characters and build a new string. The string example to loop/iterate:
The character I want to skip: Expected result:
|
December 08, 2021 Re: How to loop through characters of a string in D language? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BoQsc | 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:
The character I want to skip: Expected result:
|
December 08, 2021 Re: How to loop through characters of a string in D language? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BoQsc | 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 Re: How to loop through characters of a string in D language? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Biotronic | 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:
The character I want to skip: Expected result:
[..] I somehow have universal cross language hate for this kind of algorithm. Anyways,
|
December 08, 2021 Re: How to loop through characters of a string in D language? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D Ruppe | 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.
|
December 08, 2021 Re: How to loop through characters of a string in D language? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BoQsc | 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:
The character I want to skip: Expected result:
string b = a.replace(";", ""); |
December 08, 2021 Re: How to loop through characters of a string in D language? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bauss | 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:
The character I want to skip: Expected result:
string b = a.replace(";", ""); Thanks, that's what I used to do few years ago.
|
December 08, 2021 Re: How to loop through characters of a string in D language? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BoQsc | 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:
The character I want to skip: Expected result:
string b = a.replace(";", ""); Thanks, that's what I used to do few years ago.
It's also worth noting the differences in compiler output, as well as the time taken to compile, these two approaches: (1) (2) |
December 08, 2021 Re: How to loop through characters of a string in D language? | ||||
---|---|---|---|---|
| ||||
Posted in reply to forkit | 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) (2) 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 Re: How to loop through characters of a string in D language? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stanislav Blinov | 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. |