使用分隔符将字符串拆分为子字符串
给定一个字符串和一个分隔字符。根据分隔符拆分字符串并打印生成的子字符串列表。
例子:
Input : str = "geeks;for;geeks"
d_ch = ';'
Output : geeks
for
geeks
Input : str = "##ayush##jauhari####"
d_ch = '#'
Output : ayush
jauhari
资料来源:微软 IDC 班加罗尔采访 |设置 153。
算法:
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
word = word + str[i]
else
if word.size != 0
substr_list[num] = word
num++
word = ""
return num
该算法将填充数组substr_list[]中的拆分子字符串,并将此类子字符串的数量返回为num 。
C++
// C++ implementation to split string into
// substrings on the basis of delimiter
#include
using namespace std;
// function to split string into substrings on the
// basis of delimiter and return the substrings
// after split
vector 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 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 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 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 substr_list = new Vector();
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 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 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 substr_list = new 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.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 res = splitStrings(str, dl);
foreach (String x in res)
{
Console.WriteLine(x);
}
}
}
//This code is contributed by 29AjayKumar
Javascript
输出:
geeks
for
geeks
时间复杂度: O(n),其中n是给定字符串的长度。