LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-22-2007, 03:05 AM   #1
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
C++ problem extracting a substring - extra character


This is really simple and I don't understand why my code doesn't work properly. I have strings in the form "(a,b)", where a and b are numbers and I just want to store those strings in variables (so a in one variable, b in another and not keeping the brackets or the comma). My code looks like this (irrelevant bits omitted):

string a = second.substr(1, str.find(',', 1) - 1);
string b = second.substr(str.find(',', 1) + 1, second.length() - 2);

cout << str[str.length() - 2] << endl;

Here's an example string: (2108974854,0). Printing out the variable a is OK and I get "2108974854", but printing out b, I get "0)". What I find strange is that if I print out str[str.length() - 2] as I have done above, I get "0" as I should.

Any ideas?
 
Old 07-22-2007, 08:00 AM   #2
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
   string full = "(2108974854,0)";
   string::size_type comma_pos = full.find(',');
   string::size_type closing_paren_pos = full.find(')');

   cout << full.substr(1, comma_pos - 1) << endl;
   cout << full.substr(comma_pos + 1, full.length() - closing_paren_pos);
}

/*
 * $ ./runme
 * 2108974854
 * 0
 */
 
Old 07-22-2007, 09:12 AM   #3
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Original Poster
Rep: Reputation: Disabled
Ok, thanks. Do you have any idea why my code doesn't work?
 
Old 07-22-2007, 10:48 AM   #4
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Well, if you look at substr() you will see that the first parameter is an offset (what I call position). The last paramter is not an offset, however, it's a count from that offset. You are sending the length minus 2 as a count, which is actually > than the number of characters left from the offset. Thus, you get the entire string because substr() gives you up to count characters from the supplied offset, it doesn't crash.
 
Old 07-22-2007, 11:21 AM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Original Poster
Rep: Reputation: Disabled
Ah, thanks .
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
php preg_replace substring of a substring senyahnoj Programming 5 12-08-2006 11:31 AM
Need Help Extracting A Substring eur0dad Linux - General 6 09-21-2006 10:34 PM
Problem extracting PHP 5.1.2 source CrashedItAgain Mandriva 0 03-06-2006 08:19 AM
ownership problem while extracting tej Linux - General 2 03-22-2005 07:32 AM
Extra character in complemented file. softnayr Programming 1 09-25-2004 11:54 AM


All times are GMT -5. The time now is 05:57 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration