How to separete input="a,b,c"

To add comments, please log in or register
Cromo
259
Cromo  
input string ="a,b,c,d";

I want to know how to separate by comma, strings and print them.

For example,

I want to make output using Print like,,,,


My name is a.

I like b.

Do you know c?

I hate c.

Keith Watford
Moderator
20584
Keith Watford  
kajironpu:

I want to know how to separate by comma, strings and print them.

Use StringSplit() to place them in an array and then access the array for your prints.

Cromo
259
Cromo  
Keith Watford:

Use StringSplit() to place them in an array and then access the array for your prints.

Thanks! I will try.
Mehmet Bastem
1780
Mehmet Bastem  
kajironpu:

I want to know how to separate by comma, strings and print them.

For example,

I want to make output using Print like,,,,


My name is a.

I like b.

Do you know c?

I hate c.

//+------------------------------------------------------------------+
//|                                                        Split.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
input string to_split="a,b,c,d";
   string sep=",";                // A separator as a character 
   ushort u_sep;                  // The code of the separator character 
   string result[];               // An array to get strings 
   //--- Get the separator code 


void OnStart()
  {
//---
   u_sep=StringGetCharacter(sep,0); 
   //--- Split the string to substrings 
   int k=StringSplit(to_split,u_sep,result); 
   
   if(k>0) 
     { 
      for(int i=0;i<k;i++) 
        { 
         Print("i: ",i," = ",result[i]); 
        } 
     }

  }
//+------------------------------------------------------------------+
Anthony Garot
3310
Anthony Garot  

kajironpu:

Do you know c?

I hate c.

c isn't so bad once you get to know him.

Yohana Parmi
6971
Yohana Parmi  
Anthony Garot:

c isn't so bad once you get to know him.

:))

Petr Nosek
3059
Petr Nosek  
Mehmet Bastem:

You can shorten your code:

#property strict   
   input string to_split="a,b,c,d";
   ushort u_sep=',';              // The code of the separator character 
   string result[];               // An array to get strings 

void OnStart()
  {
   //--- Split the string to substrings
   int k=StringSplit(to_split,u_sep,result);
   for(int i=0;i<k;i++)
      Print("i: ",i," = ",result[i]);
  }
To add comments, please log in or register