How to split a string at the first occurrence of a separator in C++

2 Answers

0 votes
#include <iostream>
#include <string>

int main()
{
    std::wstring str = L"java go c c++ python c#";
	int pos = (int)str.find(L"c");
	
	std::wstring part1 = L"", part2 = L"";

	if (pos != -1) {
		part1 = str.substr(0, pos);
		part2 = str.substr(pos);
	}

	std::wcout << part1 << std::endl;
	std::wcout << part2 << std::endl;
}


 
      
/*
run:

java go 
c c++ python c#
     
*/

 



answered May 23, 2024 by avibootz
0 votes
#include <iostream>
#include <string>

int main()
{
    std::string str = "java go c c++ python c#";
	int pos = (int)str.find("c");
	
	std::string part1 = "", part2 = "";

	if (pos != -1) {
		part1 = str.substr(0, pos);
		part2 = str.substr(pos);
	}

	std::cout << part1 << std::endl;
	std::cout << part2 << std::endl;
}


 
      
/*
run:

java go 
c c++ python c#
     
*/

 



answered May 23, 2024 by avibootz

Related questions

...