Split the string into substrings using delimiter
Last Updated :
27 Jul, 2022
Given a string and a delimiter character. Split the string based on the delimiter and print the list of resulting sub strings.
Examples:
Input : str = "geeks;for;geeks"
d_ch = ';'
Output : geeks
for
geeks
Input : str = "##ayush##jauhari####"
d_ch = '#'
Output : ayush
jauhari
Source: Microsoft IDC Bangalore Interview | Set 153
Algorithm:
splitStrings(str, substr_list, dl)
- Initialize word = ""
initialize num = 0
str = str + dl
l = str.size - for i = 0 to l-1
- if str[i] != dl
- else
- if word.size != 0
- substr_list[num] = word
- num++
- word = ""
- return num
This algorithm will fill in the splitted substrings in the array substr_list[] and will return the number of such substrings as num.
C++
// C++ implementation to split string into
// substrings on the basis of delimiter
#include <bits/stdc++.h>
using namespace std;
// function to split string into substrings on the
// basis of delimiter and return the substrings
// after split
vector<string> splitStrings(string str, char dl)
{
string word = "";
// to count the number of split strings
int num = 0;
// adding delimiter character at the end
// of 'str'
str = str + dl;
// length of 'str'
int l = str.size();
// traversing 'str' from left to right
vector<string> substr_list;
for (int i = 0; i < l; i++) {
// if str[i] is not equal to the delimiter
// character then accumulate it to 'word'
if (str[i] != dl)
word = word + str[i];
else {
// if 'word' is not an empty string,
// then add this 'word' to the array
// 'substr_list[]'
if ((int)word.size() != 0)
substr_list.push_back(word);
// reset 'word'
word = "";
}
}
// return the splitted strings
return substr_list;
}
// Driver program to test above
int main()
{
string str = "geeks;for;geeks";
char dl = ';';
vector<string> res = splitStrings(str, dl);
for (auto x : res)
cout << x << endl;
return 0;
}
Java
// Java implementation to split String into
// substrings on the basis of delimiter
import java.util.*;
class GFG
{
// function to split String into subStrings
// on the basis of delimiter and return
// the subStrings after split
static Vector<String> splitStrings(String str, char dl)
{
String word = "";
// to count the number of split Strings
int num = 0;
// adding delimiter character
// at the end of 'str'
str = str + dl;
// length of 'str'
int l = str.length();
// traversing 'str' from left to right
Vector<String> substr_list = new Vector<String>();
for (int i = 0; i < l; i++)
{
// if str[i] is not equal to the delimiter
// character then accumulate it to 'word'
if (str.charAt(i) != dl)
{
word = word + str.charAt(i);
}
else
{
// if 'word' is not an empty String,
// then add this 'word' to the array
// 'substr_list[]'
if ((int) word.length() != 0)
{
substr_list.add(word);
}
// reset 'word'
word = "";
}
}
// return the splitted Strings
return substr_list;
}
// Driver code
public static void main(String[] args)
{
String str = "geeks;for;geeks";
char dl = ';';
Vector<String> res = splitStrings(str, dl);
for (String x : res)
{
System.out.println(x);
}
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python 3 implementation to split string
# into substrings on the basis of delimiter
# function to split string into substrings
# on the basis of delimiter and return the
# substrings after split
def splitStrings(st, dl):
word = ""
# to count the number of split strings
num = 0
# adding delimiter character at
# the end of 'str'
st += dl
# length of 'str'
l = len(st)
# traversing 'str' from left to right
substr_list = []
for i in range(l):
# if str[i] is not equal to the
# delimiter character then accumulate
# it to 'word'
if (st[i] != dl):
word += st[i]
else:
# if 'word' is not an empty string,
# then add this 'word' to the array
# 'substr_list[]'
if (len(word) != 0):
substr_list.append(word)
# reset 'word'
word = ""
# return the splitted strings
return substr_list
# Driver Code
if __name__ == '__main__':
str = "geeks;for;geeks"
dl = ';'
res = splitStrings(str, dl)
for x in range(len(res)):
print(res[x])
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation to split String into
// substrings on the basis of delimiter
using System;
using System.Collections.Generic;
class GFG
{
// function to split String into subStrings
// on the basis of delimiter and return
// the subStrings after split
static List<String> splitStrings(String str, char dl)
{
String word = "";
// to count the number of split Strings
int num = 0;
// adding delimiter character
// at the end of 'str'
str = str + dl;
// length of 'str'
int l = str.Length;
// traversing 'str' from left to right
List<String> substr_list = new List<String>();
for (int i = 0; i < l; i++)
{
// if str[i] is not equal to the delimiter
// character then accumulate it to 'word'
if (str[i] != dl)
{
word = word + str[i];
}
else
{
// if 'word' is not an empty String,
// then add this 'word' to the array
// 'substr_list[]'
if ((int) word.Length != 0)
{
substr_list.Add(word);
}
// reset 'word'
word = "";
}
}
// return the splitted Strings
return substr_list;
}
// Driver code
public static void Main()
{
String str = "geeks;for;geeks";
char dl = ';';
List<String> res = splitStrings(str, dl);
foreach (String x in res)
{
Console.WriteLine(x);
}
}
}
//This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript implementation to split string into
// substrings on the basis of delimiter
// function to split string into substrings on the
// basis of delimiter and return the substrings
// after split
function splitStrings(str, dl)
{
var word = "";
// to count the number of split strings
var num = 0;
// adding delimiter character at the end
// of 'str'
str = str + dl;
// length of 'str'
var l = str.length;
// traversing 'str' from left to right
var substr_list = [];
for (var i = 0; i < l; i++) {
// if str[i] is not equal to the delimiter
// character then accumulate it to 'word'
if (str[i] != dl)
word = word + str[i];
else {
// if 'word' is not an empty string,
// then add this 'word' to the array
// 'substr_list[]'
if (word.length != 0)
substr_list.push(word);
// reset 'word'
word = "";
}
}
// return the splitted strings
return substr_list;
}
// Driver program to test above
var str = "geeks;for;geeks";
var dl = ';';
var res = splitStrings(str, dl);
res.forEach(x => {
document.write( x + "<br>");
});
</script>
Time Complexity: O(n), where n is the length of the given string.
Similar Reads
Print all Substrings of length n possible from the given String Given a string str and an integer N, the task is to print all possible sub-strings of length N. Examples: Input: str = âgeeksforgeeksâ, N = 3Output: gee eek eks ksf sfo for org rge gee eek eksExplanations: All possible sub-strings of length 3 are âgeeâ, âeekâ, âeksâ, âksfâ, âsfoâ, âforâ, âorgâ, ârge
8 min read
Split given String into substrings of size K by filling elements Given a string str of length N and an integer K, the task is to split the string into K sized groups and if the last group does not have K characters remaining, then a character ch is used to complete the group. Examples: Input: str = "Algorithms", K = 3, ch = "@"Output: Alg ori thm s@@Explanation:T
5 min read
C++ String to Vector Using Delimiter Delimiters are used as separators between the characters or words in a string so that different results can get separated by the delimiter. In this article let us see the different ways in which a string with delimiters can be converted to a vector of words after getting separated by delimiters.Exam
5 min read
Extract substrings between any pair of delimiters Given a string str, the task is to extract the substrings present between two delimiters, i.e. '[' and ']'. Examples: Input: str = â[This is a string to be extracted]âOutput: This is a string to be extractedExplanation: The square brackets '[' and ']' serve as delimiters in the given string. Input:
8 min read
Check if a string can be split into substrings starting with N followed by N characters Given a string str, the task is to check if it can be split into substrings such that each substring starts with a numeric value followed by a number of characters represented by that numeric integer. Examples: Input: str = "4g12y6hunter" Output: Yes Explanation: Substrings "4g12y" and "6hunter" sat
5 min read
How to split a string in C/C++, Python and Java? Splitting a string by some delimiter is a very common task. For example, we have a comma-separated list of items from a file and we want individual items in an array. Almost all programming languages, provide a function split a string by some delimiter. In C: // Splits str[] according to given delim
7 min read