• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 343
  • Last Modified:

String compare

String A =  "there is a UDP"
String B = "UDP"

How to find out whether string A contains "UDP"?
should i use string compare or anything?
should i str compare String A and B??
Could anyone advise

Thanks
0
OwenMania
Asked:
OwenMania
1 Solution
 
grg99Commented:
man strstr

0
 
Drew_BentonCommented:
grg99 is right what function but ill explain:

void TestFunction()
{
char* A = "there is a UDP";       // Your first orig string
char* B = "UDP";                      // Your second string you're looking for

char* result = strstr(A,B);         // strstr returns a pointer of char* of the first whole occurance

if(result)                // if the pointer actually has a location now ie: the WHOLE string was found
   ;                        // String was found, so do what ever you want now
else
   ;                        // String was not found, so dont do what was planned
}

- Drew
0
 
igor_skCommented:
All you guys said is correct in C.
But, if you use C++ (I suppose you do) and you use 'string' class to store your strings. Then you should use find() method of the class string:

string A =  "there is a UDP";
string B = "UDP";

if (A.find(B) != -1)
     cout << "String A contains string B" << endl;
else
     cout << "String B was not found in string A" << endl;
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Drew_BentonCommented:
hey ques: i was thinking the same thing as igor_sk siad, but it was part of a CString class (MFC), now this string class, umm honeslty, nvr heard of it b4 in C++, so what do i need to do to use it or is it a default type?
0
 
OwenManiaAuthor Commented:
But how come i cannot declare String??
There's an error: Undeclared identifier "string"

i have already included string.h

#include <iostream>
#include <fstream.h>
#include <stdlib.h>
#include <STRING>

int main () {

String A;
return 0;
}
0
 
Drew_BentonCommented:
try:
#include <STRING.H>
then that code
0
 
Drew_BentonCommented:
then String/string
0
 
OwenManiaAuthor Commented:
i have tried but failed
0
 
OwenManiaAuthor Commented:
The method is:
  #include <string.h>
  using namespace std;

Done
0
 
Drew_BentonCommented:
Ahh here we go, take a look at this:

http://www.bgsu.edu/departments/compsci/docs/string.html
0
 
Drew_BentonCommented:
If that doesnt work, id recommend taking a look at my 1st post example of using the char*'s and try that to do what you want to do
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now