Deleting sections of a string base off delimiter

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 958 Views - Last Post: 20 December 2011 - 03:38 PM Rate Topic: -----

#1 anndruu12  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 23-March 09

Deleting sections of a string base off delimiter

Posted 19 December 2011 - 02:02 PM

If I have a string s in C++, and the string has information in it such as "Data - Test - Variable", how might I go about parsing this string and finding the second '-', and then deleting everything before and including that? So ideally, it would parse this string, find the second - and delete which would leave me with the string "Variable". I ask because "Data" and "Test" portions of the string will constantly change, but there will always be a " - " between them, and I just care about the "Variable" portion. Thanks guys!

Is This A Good Question/Topic? 0
  • +

Replies To: Deleting sections of a string base off delimiter

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Deleting sections of a string base off delimiter

Posted 19 December 2011 - 02:07 PM

Take a look at string::find_last_of member function of the c++ string class.

You can use that in conjunction with the string::substr function to extract the tail portion of the original string.
Was This Post Helpful? 1
  • +
  • -

#3 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 662
  • View blog
  • Posts: 2,273
  • Joined: 31-December 10

Re: Deleting sections of a string base off delimiter

Posted 19 December 2011 - 02:12 PM

The string member functions will be enough to parse a string, but just so you know, you could also use a stringstream to parse a string.

This post has been edited by vividexstance: 19 December 2011 - 02:12 PM

Was This Post Helpful? 1
  • +
  • -

#4 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Deleting sections of a string base off delimiter

Posted 19 December 2011 - 02:32 PM

Or if you really wanted to write generic code you could use iterators which can be used to parse both streams, strings, char*, and any other sequence that stores characters(granted you have to make sure to unset ignoring space on streams)
Was This Post Helpful? 1
  • +
  • -

#5 anndruu12  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 23-March 09

Re: Deleting sections of a string base off delimiter

Posted 19 December 2011 - 02:54 PM

I changed it up a little. I will be needing to store the "Data" portion, the "Test" portion, as well as the "Variable" portion, so the " - " are what I will need to use to move around the string. I can still use the above mentioned methods to store each portion in it's own variable? Thanks again.
Was This Post Helpful? 0
  • +
  • -

#6 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 662
  • View blog
  • Posts: 2,273
  • Joined: 31-December 10

Re: Deleting sections of a string base off delimiter

Posted 19 December 2011 - 03:12 PM

There is an overload of the getline() function which allows you to set the delimiter on the input operation. So if you used an istringstream, you could do this easily. Here is an example of splitting a comma separated string:
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
    const char DELIM = ',';
    const string string_to_parse = "Hello, world, how, are, you?";
    istringstream iss(string_to_parse);
    string line;
    while(getline(iss, line, DELIM))
    {
        cout << "Line: \"" << line << "\"\n";
    }
}


Like Ishkabible said though, you'll need to set the stream to normally skip whitespace in the beginning of input. This program will output:

Quote

./test
Line: "Hello"
Line: " world"
Line: " how"
Line: " are"
Line: " you?"

Was This Post Helpful? 1
  • +
  • -

#7 anndruu12  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 23-March 09

Re: Deleting sections of a string base off delimiter

Posted 20 December 2011 - 07:29 AM

Quote

Like Ishkabible said though, you'll need to set the stream to normally skip whitespace in the beginning of input. This program will output:

Quote

./test
Line: "Hello"
Line: " world"
Line: " how"
Line: " are"
Line: " you?"


Can you explain this a little more. How would you go about getting rid of spaces before and after what I parse?
Was This Post Helpful? 0
  • +
  • -

#8 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Deleting sections of a string base off delimiter

Posted 20 December 2011 - 07:44 AM

Quote

Like Ishkabible said though, you'll need to set the stream to normally skip whitespace in the beginning of input. This program will output:


I was talking about something like in.unsetf(std::ios::skipws); so that space wouldn't be ignored if you used an istream iterator. the advantage of using an iterator is that the same code could work for a any istream, a string, char*, or even a vector<char>.

if you use getline and want to skip over leading space then that is something completely different.

you can use
iss>>ws
seen here. this gets rid of all leading space characters.

This post has been edited by ishkabible: 20 December 2011 - 07:49 AM

Was This Post Helpful? 1
  • +
  • -

#9 anndruu12  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 23-March 09

Re: Deleting sections of a string base off delimiter

Posted 20 December 2011 - 08:38 AM

Well, that worked. But then I realized another issue. There will be times when the string may have a space in it. For example " Test Case ". In this case, if I do the skipws, it would split the string up, when what I would really want would be "Test Case". Thoughts?
Was This Post Helpful? 0
  • +
  • -

#10 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Deleting sections of a string base off delimiter

Posted 20 December 2011 - 09:12 AM

I would leave skipws alone and parse the original string using the hyphen as the delimiter. Assuming that will leave you with strings that may or may not have leading and/or trailing spaces.

Trailing spaces are easy -- examine the last character & if it's a space use string::erase to truncate the string.

For leading spaces, examine the first character and if it's a space use string::substr to extract a substring that excludes the first character.

If you want to handle the possibility that there may be more than 1 leading or trailing space, put those operations in loops that continue until they find a non-space character.
Was This Post Helpful? 1
  • +
  • -

#11 anndruu12  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 23-March 09

Re: Deleting sections of a string base off delimiter

Posted 20 December 2011 - 12:18 PM

Ok, so if I am getting the string with
getline(iss, substr, '-')



Then to exclude those spaces, I could do something like

string = string.erase((sizeof(string)-1)), 1);

string = string.substr(1, sizeof(string)-1)



Something like that?

Edit: I figured out how to delete the trailing space using erase and find_last_of . Now I am stumped on getting rid of the leading space.

This post has been edited by anndruu12: 20 December 2011 - 12:26 PM

Was This Post Helpful? 0
  • +
  • -

#12 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 662
  • View blog
  • Posts: 2,273
  • Joined: 31-December 10

Re: Deleting sections of a string base off delimiter

Posted 20 December 2011 - 01:11 PM

Just check if the first character is whitespace, you can manually check if it equals an actual space character(str[0] == ' '), or you can use a C library function from the <cctype> header called "isspace()".
Here's a reference to the <cctype> header. There are some other useful functions on that page and throughout that website so check it out.

If you have whitespace at the beginning of the string, then just start a loop that iterates until it reaches a NON-whitespace character. When that happens, you will know how many characters you need to remove. Just call the string::erase() function with the position in the string to start erasing from, and the second argument is just the number of characters to erase.
Was This Post Helpful? 1
  • +
  • -

#13 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Deleting sections of a string base off delimiter

Posted 20 December 2011 - 02:18 PM

You could use cctype but there's really no reason to be including an entire collection of standard C functions just to check for a space.

#include <iostream>
#include <string>
using namespace std;

int main() {
    string str = "   my string     ";
    // to remove the leading space
    while( str[0] == ' ' ) {
        str = str.substr( 1, str.size()-1 );
    }
    // to removing the trailing space
    while( *(str.end()-1) == ' ' ) {  //alternatively:   while( str[str.size()-1] == ' ' ) {
        str.erase( str.end()-1 );    // note A
    }
    cout << "---" << str << "---" << endl;
    return 0;
}



note A: str.end() returns an iterator (which functions like a pointer) to the memory location immediately following the end of the string. You can do arithmetic on iterators just as you can on pointers, so str.end()-1 is a reference to the last character of the string.
Was This Post Helpful? 0
  • +
  • -

#14 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Deleting sections of a string base off delimiter

Posted 20 December 2011 - 02:26 PM

Quote

note A: str.end() returns an iterator (which functions like a pointer) to the memory location immediately following the end of the string. You can do arithmetic on iterators just as you can on pointers, so str.end()-1 is a reference to the last character of the string.


sub note: you can only do that on random access iterators. you can still however use -- on bidirectional iterators.

also, isspace checks for spaces, tabs, newlines and some other stuff. so it dose more than just == ' '

edit: lol, i said basically the same thing vividexstance said sans the code.

This post has been edited by ishkabible: 20 December 2011 - 02:33 PM

Was This Post Helpful? 0
  • +
  • -

#15 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 662
  • View blog
  • Posts: 2,273
  • Joined: 31-December 10

Re: Deleting sections of a string base off delimiter

Posted 20 December 2011 - 02:26 PM

View Postr.stiltskin, on 20 December 2011 - 04:18 PM, said:

note A: str.end() returns an iterator (which functions like a pointer) to the memory location immediately following the end of the string. You can do arithmetic on iterators just as you can on pointers, so str.end()-1 is a reference to the last character of the string.


This is a little misleading because there are different types or "categories" of iterators, and not all of them will allow you to do the same arithmetic on them. For example, some iterators will only let you go forward, but not backwards. And to complain about the cctype header being included is kind of ridiculous. It's a small C header, it's not like your including iostream or anything. I wasn't going to just post an answer, but here is what I think is somewhat more concise and less cryptic to a person learning the language:
// StripLeadingWhitespace.cpp
#include <iostream>
#include <sstream>
#include <string>
#include <cctype>

using namespace std;

void stripLeadingWhitespace(string&);

int main(int argc, char* argv[])
{
	const string input("Hello, how, are, you, doing, today?");
	istringstream iss(input);
	string line;
	while(getline(iss, line, ','))
	{
		stripLeadingWhitespace(line);
		cout << "line: \"" << line << "\"\n";
	}
}

void stripLeadingWhitespace(string& s)
{
	int i;
	for(i = 0; i < s.size(); ++i)
	{
		if(!isspace(s[i]))
			break;
	}
	s.erase(0, i);
}



*EDIT*: I also forgot to mention that the "isspace()" function doesn't just check for a space, it also checks for tabs and any other whitespace.

This post has been edited by vividexstance: 20 December 2011 - 02:29 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2