title: How to Cut String C++
body: <p>Hello i want to cut string in this situation:</p>
  
  <pre><code>if (std::count(text.begin(), text.end(), ',') &gt; 3 {
     //Cut String after 3 ',' 
  }
  </code></pre>
  
  <p>text look like this:</p>
  
  <p>"item1, item2, item3, item4, item5"</p>
  
  <p>and i want this:</p>
  
  <p>"item1, item2, item3, item4[Cut everything out on the right side >>], item5"
  and return
  "item1, item2, item3, item4"</p>
  
  <p>Looking on first answer im looking for something like this(but working):</p>
  
  <pre><code>#include &lt;string.h&gt; 
  #include &lt;iostream&gt; 
  using namespace std; 
  
  int main() 
  { 
      string s1 = "item1, item2, item3, item4, item5"; 
  
      if(std::count(s1.begin(), s1.end(), ',') &gt; 3) {
          int comma = 0;
          int x = sizeof(s1);
          for(int i = 0; i &lt; x; i++) {
              if (comma == 3) {
                  string sub = s1.substr(0, i); 
                  cout &lt;&lt; "String is: " &lt;&lt; sub; 
              }
              if(s1[i] == ',') {
                  comma++;
              }
          }
      }
      return 0; 
  } 
  </code></pre>