Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
WillLTT

[C++] split string on "name=test" and get "test" into a string without "name="

41 minutes ago, WillLTT said:

im doing standard vs19 c++.

 

im trying to split


//"test" is changed by what user input is.
std::string name = "name=test";

into a string and get "test" into a string. however i dont want "name=" in there.

and then cout the result


std::cout << name;

so basically remove "name=" from the string.

One possibility:

#include <iostream>
#include <string>

int 
main()
{
	const std::string name = "name=test";

	//check if "name=" is present in string and if it starts at position 0.
	const auto startIndex = name.find("name=");
	if (startIndex == std::string::npos || startIndex)
	{
		std::cout << "Did not find \"name=\" or it was not located at beginning of string.\n";
		return 1;
	}	

	//split string.
	const auto subName = name.substr(std::string("name=").length());

	//optionally test result length. Will be empty if there's nothing after "name="
	if (!subName.length())
	{
		std::cout << "Result empty.";
		return 2;
	}

	std::cout << subName << '\n';

	return 0;
}

 

Recommended Posts

Posted · Original PosterOP

im doing standard vs19 c++.

 

im trying to split

//"test" is changed by what user input is.
std::string name = "name=test";

into a string and get "test" into a string. however i dont want "name=" in there.

and then cout the result

std::cout << name;

so basically remove "name=" from the string.

Link to post
Share on other sites

@WillLTT   Sorry but I am confused by your question.  Do you want a count of characters after "name="?  Or do you want the value after "name="?

 

Does "name=" always exist?   If so that would simply the code tremendously.

 

Link to post
Share on other sites
Posted · Best Answer
41 minutes ago, WillLTT said:

im doing standard vs19 c++.

 

im trying to split


//"test" is changed by what user input is.
std::string name = "name=test";

into a string and get "test" into a string. however i dont want "name=" in there.

and then cout the result


std::cout << name;

so basically remove "name=" from the string.

One possibility:

#include <iostream>
#include <string>

int 
main()
{
	const std::string name = "name=test";

	//check if "name=" is present in string and if it starts at position 0.
	const auto startIndex = name.find("name=");
	if (startIndex == std::string::npos || startIndex)
	{
		std::cout << "Did not find \"name=\" or it was not located at beginning of string.\n";
		return 1;
	}	

	//split string.
	const auto subName = name.substr(std::string("name=").length());

	//optionally test result length. Will be empty if there's nothing after "name="
	if (!subName.length())
	{
		std::cout << "Result empty.";
		return 2;
	}

	std::cout << subName << '\n';

	return 0;
}

 

Link to post
Share on other sites
Posted · Original PosterOP
46 minutes ago, PyroTheWise said:

@WillLTT   Sorry but I am confused by your question.  Do you want a count of characters after "name="?  Or do you want the value after "name="?

 

Does "name=" always exist?   If so that would simply the code tremendously.

 

@PyroTheWise

i want "test"

Correct "name=" does always exist.

Correct i want the value after "name="

 

Sorry for writing a bad question

Link to post
Share on other sites

if its always "x=y" and x & y are strings that dont contain "=" you could just split the string with '=' as delimiter and take the last part of the result

 

"pseudo code" 

1)

input : String = "x=bla"

inputSplitted : String = input.split("=")

result : String = inputSplitted[1] //would give you "bla"

 

2)

if its always "name=y" and y is any kind of string you should just do 

input : String = "name=test"

result : String  = input.remove("name=")

 

3) 

if "x=y" and you have a set of values that x could be you could use regex (would work for version 1/2 aswell)

"test=y", "name=y", "tag=y" etc

 

you could do something like 

patter := "(test|name|tage)=(.*)"

where "(x_1|x_2|x_3|...|x_n") contains all your x_i of your set of possbile "tags"

and use regexp to get the match for your "y" string

Link to post
Share on other sites

@WillLTT The simple solution I was leaning towards was provided by @Unimportant

 

A simple substring since you always have "name=":

 

4 hours ago, Unimportant said:

const auto subName = name.substr(std::string("name=").length());

 

you could further simply that to const auto subName = name.substr(5); //5 is the lenght of name=, since it always exists

Link to post
Share on other sites

I'm curious (all I do is PowerShell), why wouldn't you have the user input saved to a variable of name, then look inside the variable for their input?

Link to post
Share on other sites
Posted · Original PosterOP
13 hours ago, TheRealShadoh said:

I'm curious (all I do is PowerShell), why wouldn't you have the user input saved to a variable of name, then look inside the variable for their input?

i am storing it in a variable. it just wasnt included in the post. but i want to keep the name for next app startup, so i write it to a INI file. But i want to write readable INI. with obvious places for names etc.

 

so the line written in in the ini file:

name=WillLTT

but i dont want name in the name, so i split away "name=" to get back "WillLTT"

Link to post
Share on other sites
5 minutes ago, WillLTT said:

i am storing it in a variable. but i want to keep the name for next app startup, so i write it to a INI file. But i want to write readable INI. with obvious places for names etc.

 

so the line written in in the ini file:


name=WillLTT

but i dont want name in the name, so i split away "name=" to get back "WillLTT"

So you're trying to parse an INI file? If you're doing this on Windows, there's already an API that'll do all the parsing for you without you having to handle the actual file contents: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getprivateprofilestring


Wife's build: Amethyst - Intel i7-5820k, 16GB EVGA DDR4, ASUS X99-PRO/USB 3.1, EVGA GTX 1080 SC, Corsair Obsidian 750D, Corsair RM1000

My build: Mira - Intel i7-5820k, 16GB EVGA DDR4, ASUS Sabertooth X99, EVGA GTX 1070 SC Black Edition, NZXT H440, EVGA Supernova 1050 GS

Link to post
Share on other sites
Posted · Original PosterOP
Just now, brandishwar said:

So you're trying to parse an INI file? If you're doing this on Windows, there's already an API that'll do all the parsing for you without you having to handle the actual file contents: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getprivateprofilestring

im not asking question here, i was telling @TheRealShadohwhat i was doing.

 

The problem has been solved already.

 

Sorry for any confusion.

Link to post
Share on other sites
1 minute ago, WillLTT said:

im not asking question here, i was telling @TheRealShadohwhat i was doing.

 

The problem has been solved already.

 

Sorry for any confusion.

Right, and now that I know why you're wanting to parse the example line in question, knowing where it came from, I'm pointing you to an easier method of reading and writing the .INI file that abstracts away reading the file, reading the line, and parsing the line. Sometimes knowing the full scope of the problem you're trying to solve allows us to point you to solutions that are a lot simpler than whatever you're considering.

 

So if you're now going to be trying to implement a .ini file parser, reconsider doing that.


Wife's build: Amethyst - Intel i7-5820k, 16GB EVGA DDR4, ASUS X99-PRO/USB 3.1, EVGA GTX 1080 SC, Corsair Obsidian 750D, Corsair RM1000

My build: Mira - Intel i7-5820k, 16GB EVGA DDR4, ASUS Sabertooth X99, EVGA GTX 1070 SC Black Edition, NZXT H440, EVGA Supernova 1050 GS

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×