Easy: return specific position of String

 

this is confusing me a bit,

string fruit= "Tom today eat some Apple and he is happy";

return the 5th position/word were meant to be the "apple"

i couldn't find anything useful at  https://www.mql5.com/en/docs/strings

my focus is on mql5

Documentation on MQL5: String Functions
Documentation on MQL5: String Functions
  • www.mql5.com
String Functions - Reference on algorithmic/automated trading language for MetaTrader 5
 
mohdqallaf: i couldn't find anything useful at  https://www.mql5.com/en/docs/strings

There is no fifth position unless you split the string into atoms. Go back and read again.

 
mohdqallaf :

this is confusing me a bit,

return the 5th position/word were meant to be the "apple"

i couldn't find anything useful at  https://www.mql5.com/en/docs/strings

my focus is on mql5

Simple example:

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2018, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property version "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   string text="'Tom today eat some Apple and he is happy'";
   Print("String in which search is made: ",text);
   PrintFormat("'T' position %d , 'today' position %d ",StringFind(text,"T",0),StringFind(text,"today",0));
  }

result:

String in which search is made: Tom today eat some Apple and he is happy
'T' position 0 , 'today' position 4 


Help: StringFind

Documentation on MQL5: String Functions / StringFind
Documentation on MQL5: String Functions / StringFind
  • www.mql5.com
String Functions / StringFind - Reference on algorithmic/automated trading language for MetaTrader 5
Files:
1.mq5  2 kb
 
William Roeder:

There is no fifth position unless you split the string into atoms. Go back and read again.

How about you go back and read the OP again? It clearly states that they are trying to extract the fifth word in the sentence, "Apple". It doesn't get any more clear than that. What about that description was unclear to you?!


OP, you will need to split the string by the space character. That will result in an array of strings which you can then access by index. 

void OnStart(){
   string sentence = "Tom today eat some Apple and he is happy";
   string words[];
   int word_count = StringSplit(sentence, ' ', words);
   Print(words[4]); //Apple
}


 

 
nicholish en: How about you go back and read the OP again? It clearly states that they are trying to extract the fifth word in the sentence, "Apple". It doesn't get any more clear than that. What about that description was unclear to you?!

I don't need to go back; OP was explicit. Nothing was unclear to me.

My "go back" (post #1) is where I answered OP about getting atoms, that he needs to go back and re-read the strings page where he didn't get anything useful, and I specifically mentioned split.

 
William Roeder:

...and I specifically mentioned split.

In the most cryptic and unhelpful way possible. Thanks Bill!

 

here is my solution after understanding your help

string StringGetPostion(string String, const int Position=0)
  {
//String="Tom today eat some Apple and he is happy"; // A string to split into substrings
   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
   u_sep=StringGetCharacter(sep,0);
//--- Split the string to substrings
   int k=StringSplit(String,u_sep,result);

   return(result[Position]);
  }


thanks every one its SOLVED