| Thread overview | ||||||||
|---|---|---|---|---|---|---|---|---|
  | 
November 09, 2020 Extract sub string from a string | ||||
|---|---|---|---|---|
  | ||||
Hi All, Request your help on how to extract sub string from a string, below is an example in PHP, need your help on how to do the same in D. $text = "welcome2worldinfo"; $hg = strtoupper(substr($text , 0, 7).'-'.substr($text, 7, 1).'-'.substr($text, 8, 5)); print_r($hg) \\ Output : WELCOME-2-WORLD From, Vino.B  | ||||
November 09, 2020 Re: Extract sub string from a string | ||||
|---|---|---|---|---|
  | ||||
Posted in reply to Vino  | On Monday, 9 November 2020 at 16:00:28 UTC, Vino wrote:
> Hi All,
>
>    Request your help on how to extract sub string from a string, below is an example in PHP, need your help on how to do the same in D.
>
> $text = "welcome2worldinfo";
> $hg = strtoupper(substr($text , 0, 7).'-'.substr($text, 7, 1).'-'.substr($text, 8, 5));
> print_r($hg) \\ Output : WELCOME-2-WORLD
>
> From,
> Vino.B
Strings in D are actually immutable arrays of chars, so you can slice them to get a substring:
import std.string : toUpper;
string text = "welcome2worldinfo";
string hg = toUpper(text[0..7] ~ "-" ~ text[7..8] ~ "-" ~ text[8..13]);
writeln(hg);
 | |||
November 09, 2020 Re: Extract sub string from a string | ||||
|---|---|---|---|---|
  | ||||
Posted in reply to Vino  | On Monday, 9 November 2020 at 16:00:28 UTC, Vino wrote: > Hi All, > > Request your help on how to extract sub string from a string, below is an example in PHP, need your help on how to do the same in D. > > $text = "welcome2worldinfo"; > $hg = strtoupper(substr($text , 0, 7).'-'.substr($text, 7, 1).'-'.substr($text, 8, 5)); > print_r($hg) \\ Output : WELCOME-2-WORLD > > From, > Vino.B Some like this: substr($text , 0, 7) // --> text[ 0 .. 7 ] substr($text, 7, 1) // --> text[ 7 .. 7+1 ] substr($text, 8, 5) // --> text[ 8 .. 8+5 ] $hg = a . b . c // --> string hg = a ~ b ~ c; strtoupper( s ) // --> s.toUpper print_r($hg) // --> writeln( hg ) And import names: import std.uni : toUpper; import std.stdio : writeln; Help: https://dlang.org/phobos/std_uni.html https://dlang.org/phobos/std_stdio.html https://dlang.org/spec/arrays.html#strings  | |||
November 09, 2020 Re: Extract sub string from a string | ||||
|---|---|---|---|---|
  | ||||
Posted in reply to k2aj  | On 11/9/20 9:53 AM, k2aj wrote:
> string text = "welcome2worldinfo";
> string hg = toUpper(text[0..7] ~ "-" ~ text[7..8] ~ "-" ~ text[8..13]);
If those concatenations with the ~ operators prove to be costly at runtime, the following range expression may be faster because it does not allocate any memory:
import std.string;
import std.range;
import std.algorithm;
import std.stdio;
void main() {
  string text = "welcome2worldinfo";
  auto hg = chain(text[0..7], only('-'), text[7..8], only('-'), text[8..13]).map!toUpper;
  writeln(hg);
}
Ali
 | |||
November 09, 2020 Re: Extract sub string from a string | ||||
|---|---|---|---|---|
  | ||||
Posted in reply to Ali Çehreli  | On Monday, 9 November 2020 at 18:55:44 UTC, Ali Çehreli wrote:
> On 11/9/20 9:53 AM, k2aj wrote:
>
> > string text = "welcome2worldinfo";
> > string hg = toUpper(text[0..7] ~ "-" ~ text[7..8] ~ "-" ~
> text[8..13]);
>
> If those concatenations with the ~ operators prove to be costly at runtime, the following range expression may be faster because it does not allocate any memory:
>
> import std.string;
> import std.range;
> import std.algorithm;
> import std.stdio;
>
> void main() {
>
>   string text = "welcome2worldinfo";
>   auto hg = chain(text[0..7], only('-'), text[7..8], only('-'), text[8..13]).map!toUpper;
>   writeln(hg);
> }
>
> Ali
Hi Both,
  Thank you very much, your solution resolved our issue.
 | |||
November 09, 2020 Re: Extract sub string from a string | ||||
|---|---|---|---|---|
  | ||||
Posted in reply to Ali Çehreli  | On Monday, 9 November 2020 at 18:55:44 UTC, Ali Çehreli wrote:
> On 11/9/20 9:53 AM, k2aj wrote:
>
> > string text = "welcome2worldinfo";
> > string hg = toUpper(text[0..7] ~ "-" ~ text[7..8] ~ "-" ~
> text[8..13]);
>
> If those concatenations with the ~ operators prove to be costly at runtime, the following range expression may be faster because it does not allocate any memory:
>
> import std.string;
> import std.range;
> import std.algorithm;
> import std.stdio;
>
> void main() {
>
>   string text = "welcome2worldinfo";
>   auto hg = chain(text[0..7], only('-'), text[7..8], only('-'), text[8..13]).map!toUpper;
>   writeln(hg);
> }
>
> Ali
I am responding to this just for my personal records. I wish we could've a "add to favorites" option here.
 | |||
Copyright © 1999-2021 by the D Language Foundation
 
Permalink
Reply