Home c++ Split string on array elements through the desired symbol

Split string on array elements through the desired symbol

Author

Date

Category

Tell me, and how to use & lt; vector & gt; To separate the rows of the rows on the substring, which will be entered into an array, no?

Need to:

“hello; how; how”

Get:

str [0] = "hello";
STR [1] = "How";
STR [2] = "Cases";

As in PHP – Explode, and C # – Split


Answer 1, Authority 100%

It seems that you wanted:

# include & lt; iostream & gt;
#Include & lt; vector & gt;
#Include & lt; String & GT;
Using Namespace STD;
INT MAIN ()
{
 Vector & lt; String & GT; ARR;
 String str ("Hi; how; how");
 String Delim (";");
 Size_t Prev = 0;
 Size_t Next;
 Size_t Delta = Delim.Length ();
 While ((Next = Str.find (Delim, Prev))! = String :: NPOS) {
  // Debug-start
  String TMP = Str.Substr (Prev, Next-Prev);
  COUT & LT; & LT; TMP & LT; & LT; Endl;
  // Debugging-End
  arr.push_back (str.Substr (Prev, Next-Prev));
  Prev = Next + Delta;
 }
 // Debug-start
 String TMP = Str.Substr (Prev);
 COUT & LT; & LT; TMP & LT; & LT; Endl;
 // Debugging-End
 arr.push_back (str.Substr (Prev));
 Return 0;
}

Answer 2, Authority 44%

Standard method No. You need 1) or write your function, 2) or connect an external library, 3) or convert std :: string in c_str and use Strtok . For example,

char * s = new char [source.size () + 1];
STRCPY (S, SOURCE.C_STR ());
char * p = STRTOK (S, ";");
While (P! = NULL) {
  COUT & LT; & LT; P & LT; & lt; Endl;
  P = STRTOK (NULL, ";");
}
delete [] s;

Answer 3, Authority 33%

More “C++ ” The method is as follows:

std :: string sentence = "hello how are you";
STD :: ISTRINGSTREAM ISS (SENENCE);
Std :: Vector & lt; Std :: String & GT; tokens;
STD :: Copy (STD :: Istream_Iterator & LT; STD :: String & GT; (ISS),
     STD :: IStream_Iterator & LT; STD :: STRING & GT; (),
     std :: back_inserter & lt; std :: vector & lt; std :: string & gt; & gt; (tokens));

In this case, the truth cannot be specified its own separator. As a more convenient alternative, I can offer Boost :: String_algo .


Answer 4, Authority 22%

Option 1

# include & lt; iostream & gt;
#Include & lt; String & GT;
Using Namespace STD;
INT MAIN ()
{
  String Base_str = ",,, 1 Tin, Tomn TYT 65, GG, GH,"; // Sample string
  String SEP = ","; // string or separator symbol
  size_t sep_size = sep.size ();
  String Temp;
  While (True) {
    temp = base_str.substr (0, base_str.find (SEP));
    If (temp.size ()! = 0) // You can add add. Check for lines from spaces
      COUT & LT; & LT; TEMP & LT; & LT; Endl; // You can insert into an array instead of the room in the output stream
    if (temp.size () == base_str.size ()) {
      Break;
    }
    ELSE {
      Base_str = base_str.substr (temp.size () + sep_size);
    }
  }
  Return 0;
}

Option 2

# include & lt; iostream & gt;
#Include & lt; String & GT;
Using Namespace STD;
INT MAIN ()
{
  String Base_str = ",, 11, 2222, 33333.44,"; // Source string
  String Delim = ","; // string or separator symbol
  Size_t POS = 0;
  size_t base_str_size = base_str.size ();
  Size_t DELIM_SIZE = DELIM.SIZE ();
  String Temp;
  While (POS & LT; BASE_STR_SIZE) {
    Temp = temp.assign (base_str, pos, base_str.find (DELIM, POS) - POS);
    if (temp.size () & gt; 0) // Checking on an empty string if necessary
    COUT & LT; & LT; TEMP & LT; & LT; Endl;
    POS + = temp.size () + DELIM_SIZE;
  }
  Return 0;
}

Answer 5, Authority 11%

C++ 11 with regexp

std :: string value = "hello, world";
static const std :: regex rdelim {","};
Std :: Vector & lt; Std :: String & GT; STRPAIRS {
    STD :: SREGEX_TOKEN_ITERATOR (VALUE.BEGIN (), VALUE.End (), Rdelim, -1),
    Std :: sregex_token_terator ()
  };

Answer 6

Such implementation works even on MSVC2010

std :: vector & lt; std :: string & gt; & amp; split (const std :: string & amp; s, char delim, std :: vector & lt; std :: string & gt; & amp; elems)
{
  STD :: STRINGSTREAM SS (S);
  STD :: STRING ITEM;
  While (STD :: GetLine (SS, Item, Delim))
  {
    elems.push_back (Item);
  }
  Return Elems;
}
Std :: Vector & lt; Std :: String & GT; Split (Const Std :: String & Amp; S, Char Delim)
{
  Std :: Vector & lt; Std :: String & GT; Elems;
  SPLIT (S, DELIM, ELEMS);
  Return Elems;
}
Int Main (int argc, char ** argv)
{
  STD :: STRING STEXT;
  STD :: GetLine (STD :: CIN, STEXT);
  Std :: Vector & lt; Std :: String & GT; Swords = Split (Stext, ';');
  Return 0;
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions