Just another example of missing string interpolation
bachmeier
no at spam.net
Fri Oct 13 14:31:01 UTC 2023
On Friday, 13 October 2023 at 14:25:34 UTC, bachmeier wrote:
> On Friday, 13 October 2023 at 12:04:18 UTC, kdevel wrote:
>> On Thursday, 12 October 2023 at 21:45:55 UTC, bachmeier wrote:
>>>> [...]
>>>> What if `s` is `"${a}"` and `subs` is `["a": "${b}", "b":
>>>> "x"]`?
>>>
>>> That's the reason it needs to be supported by the language.
>>
>> The problem with your code is that it loops over the wrong
>> list.
>> It should iterate over the variable occurrences in the string
>> `s`.
>
> That depends. If you don't want to do all of the substitutions,
> either because you potentially have `${something}` as part of
> the final string, or because you don't want to do all
> substitutions at once, those cases are trivially handled by my
> function. It works for what I do.
An example of a pattern I sometimes use:
```
string foo(string s) {
// Do a computation and substitute the result into s
// return the new s
}
string bar(string s) {
// Do a computation and substitute the result into s
// return the new s
}
string s = "String needing interpolation to insert ${y} and
${x}.";
string s2 = foo(s);
string s3 = bar(s2);
```
More information about the Digitalmars-d
mailing list