How to split a string in C/C++, Python and Java?

Splitting a string by some delimiter is an exceptionally normal errand. For instance, we have a comma-isolated rundown of things from a document and we need individual things in a cluster. Practically all programming dialects, give a capacity to split a string by some delimiter. Make a stride up from those “Hi World” programs. Figure out how to carry out information structures like Heap, Stacks, Linked List, and some more! Look at our Data Structures in C course to begin adapting today.

How to Split strings in C++?

This subject will examine how we can divide given strings into a solitary word in the C++ programming language. At the point when we partition a gathering of words or string assortments into single words, it is named the split or division of the string. Be that as it may, dividing strings is just conceivable with some delimiters like blank area ( ), comma (,), a dash (- ), and so forth, making the words a person. Moreover, there is no predefined parted capacity to separate the assortment of strings into a singular string. Along these lines, here we will become familiar with the various techniques to divide strings into a solitary one in C++.

Distinctive technique to accomplish the parting of strings in C++

  • Use strtok() capacity to divide strings
  • Utilize uniquely split() capacity to divide strings
  • Use std::getline() capacity to part string
  • Use find() and substr() capacity to part string

In C:

// Splits str[] according to given delimiters.
// and returns next token. It needs to be called
// in a loop to get all tokens. It returns NULL
// when there are no more tokens.
char * strtok(char str[], const char *delims);
// A C/C++ program for splitting a string
// using strtok()
#include <stdio.h>
#include <string.h>
int main()
    char str[] = "Geeks-for-Geeks";
    // Returns first token
    char *token = strtok(str, "-");
    // Keep printing tokens while one of the
    // delimiters present in str[].
    while (token != NULL)
        printf("%s\n", token);
        token = strtok(NULL, "-");
    return 0;
Output: Geeks

In C++

Note:  The main disadvantage of strtok() is that it only works for C style strings.
       Therefore we need to explicitly convert C++ string into a char array.
       Many programmers are unaware that C++ has two additional APIs which are more elegant
       and works with C++ string.

Method 1: Using  stringstream API of C++

Prerequisite:  stringstream API

stringstream object can be initialized using a string object, it automatically tokenizes strings on space char. Just like “cin” stream stringstream allows you to read a string as a stream of words.

Some of the Most Common used functions of StringStream.
clear() — flushes the stream 
str() —  converts a stream of words into a C++ string object.
operator << — pushes a string object into the stream.
operator >> — extracts a word from the stream.

The code below demonstrates it.

#include <bits/stdc++.h>
using namespace std;
// A quick way to split strings separated via spaces.
void simple_tokenizer(string s)
    stringstream ss(s);
    string word;
    while (ss >> word) {
        cout << word << endl;
int main(int argc, char const* argv[])
    string a = "How do you do!";
    // Takes only space separated C++ strings.
    cout << endl;
    return 0;
Output : How 

Method 2: Using C++ find() and substr() APIs.

Prerequisite: find function and substr().

This method is more robust and can parse a string with any delimiter, not just spaces(though the default behavior is to separate on spaces.) The logic is pretty simple to understand from the code below.

#include <bits/stdc++.h>
using namespace std;
void tokenize(string s, string del = " ")
    int start = 0;
    int end = s.find(del);
    while (end != -1) {
        cout << s.substr(start, end - start) << endl;
        start = end + del.size();
        end = s.find(del, start);
    cout << s.substr(start, end - start);
int main(int argc, char const* argv[])
    // Takes C++ string with any separator
    string a = "Hi$%do$%you$%do$%!";
    tokenize(a, "$%");
    cout << endl;
    return 0;
Output: Hi 

Method 3: Using  temporary string

If you are given that the length of the delimiter is 1, then you can simply use a temp string to split the string. This will save the function overhead time in the case of method 2.

#include <iostream>
using namespace std;
void split(string str, char del){
    // declaring temp string to store the curr "word" upto del
      string temp = "";
      for(int i=0; i<(int)str.size(); i++){
        // If cur char is not del, then append it to the cur "word", otherwise
          // you have completed the word, print it, and start a new word.
         if(str[i] != del){
            temp += str[i];
            cout << temp << " ";
              temp = "";
      cout << temp;
int main() {
    string str = "geeks_for_geeks";    // string to be split
     char del = '_';    // delimiter around which string is to be split
      split(str, del);
    return 0;


geeks for geeks

In Java :
In Java, split() is a method in the String class.

// expregexp is the delimiting regular expression; 
// limit is the number of returned strings
public String[] split(String regexp, int limit);

// We can call split() without limit also
public String[] split(String regexp)
// A Java program for splitting a string
// using split()
public class Test
    public static void main(String args[])
        String Str = new String("Geeks-for-Geeks");
        // Split above string in at-most two strings 
        for (String val: Str.split("-", 2))
        // Splits Str into all possible tokens
        for (String val: Str.split("-"))




In Python:
The split() method in Python returns a list of strings after breaking the given string by the specified separator.

  // regexp is the delimiting regular expression; 
  // limit is limit the number of splits to be made 
  str.split(regexp = "", limit = string.count(str))
line = "Geek1 \nGeek2 \nGeek3"
print(line.split(' ', 1))


['Geek1', 'Geek2', 'Geek3']
['Geek1', '\nGeek2 \nGeek3']

Also Read: How to get file size in Python?

Leave a Reply

Your email address will not be published. Required fields are marked *