Jump to content
  • Advertisement
Sign in to follow this  
Dominic Hughes

How do I compare an entire string with an specific integer value?

This topic is 760 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi their I've been using C++ for about 4+ years now on an off 
and for the life of me I've never had the need too compare strings with specific integers especially 
from a text file but now is the day.
I've done some searching around and it yields no useful results apart from conversions between integers and strings
which frankly I'm not quite shore if it's needed or not anyways Id label myself as a in the door expert on c++ 
working my way to the finish line of full expert so you can be complex-ish but Id prefer as simple as possible, 
I've looked at the c++ specification in the string class part and yea all I could find was a function call string.find()
tried that and it was only giving me the std::string::npos of the converted interger to string not the actual interger value
since I need that interger value to write too a text file on a specific line for my tilemap setter and getter. 
here is some of the code not all of it but gives you the idea that I've mostly tried:
#include <iostream>
#include <string>

int main() {

  std::string fudgepacket = "The Packet of Fudge";
  std::string fudgepacketstringtobefoundIthink = "Fudge";
  std::size_t found =  fudgepacket,find(fudgepacketstringtobefoundithink);

  int thatvalueofmight = 10; 
   std::string thatstringmight = "hello their this is 10"; 
 std::string mightyvalueofstring = std::to_string(thatvalueofmight);

    if(found == std::string::npos)
          std::cout << "You've Found Me, Packet!";

   return 0;

So how do I do it , how do I compare a std::string with a specific integer 
I was hoping for a function like 
strnum(stringcontents, integervalue); 
std::string stringcontents = "THIS IS 10";

int  integervalue = 10; 
int main() {
  if(strnum(stringcontents, integervalue))
         std::cout << "Found the Number !" << std::endl;
        std::cout << "Found the Number  ! = " << Numthatwasfound << std::endl;

Edited by x_Rosa_x

Share this post

Link to post
Share on other sites
If you want to find *any* number in a string:

That calls for std::regex(R"\b\d+\b") and std::regex_match(string, regex).


Edit: fixed regex to use raw string literal and add quantifier.

Edited by Bregma

Share this post

Link to post
Share on other sites

Both are good, each is better for different scenarios.



The built-in string::find() function is quite fast for simple direct searches.  It is a direct scan of the string searching for the value.  Unfortunately it is quite inflexible, it works only with an exact match of a single string.

The regex solution involves a lot of behind-the-scenes work. It is a somewhat heavyweight call and takes far longer than the direct scan. However, it is extremely flexible, working with any regular expression pattern. 

In this case where the person is looking for a specific known string literal, string::find() is the better solution.  If he were looking for a more general pattern of any digit, the regex solution is better.

Share this post

Link to post
Share on other sites

frob: I'm trying to compare an entire string "with file contents inside" with a integer value "index value from file scanning each line and getting the pre-set index
to use as a the line index used for replacing lines in the text file contents with new content like different positions,etc":

Text file contents:

1 1 XPOS 320 YPOS 294 D 0.50
2 1 XPOS 620 YPOS 494 D 0.18
3 1 XPOS 420 YPOS 512 D 0.18
4 1 XPOS 520 YPOS 512 D 0.18
5 1 XPOS 620 YPOS 512 D 0.40
6 1 XPOS 720 YPOS 512 D 0.18
7 1 XPOS 320 YPOS 530 D 0.50
8 1 XPOS 420 YPOS 530 D 0.18
9 1 XPOS 520 YPOS 530 D 0.20
10 1 XPOS 620 YPOS 530 D 0.18
11 1 XPOS 720 YPOS 530 D 0.10
12 1 XPOS 820 YPOS 530 D 0.2
// do stuff to get strings and indexes and then :
if(strnum(filecontentsstring, indexvalue)) 
   // replace this line in the file with another line here .
Edited by x_Rosa_x

Share this post

Link to post
Share on other sites
If you want to do some serious processing on the file, I would parse the entire thing into appropriate data structures when you load it. Then manipulate the data structures. Then write it all back to a file.

This will avoid constantly text-manipulating the full string if you want to make more than one manipulation.


struct Entry
  int lineNumber;
  int whateverThatSecondNumberMeans;
  int xPos;
  int yPos;
  float d;

std::vector<Entry> lines;

// 1. parse the entire file into the lines vector.  For the format example you gave this will be trivial.
// 2. edit the data any way you want
// 3. write the entire vector back into a file.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!