Can anybody please let me know how to check for a particular string in antoher string using VC++ 2003?
For Eg:
Soruce string - "I say that the site referenced here is not in configuration database. need to check";
String to find - "the site referenced here is not in configuration database"
can anybody please help me with this? Let me know if anymore clarity required.
std::string::find might meet what you want, see this
string s1 = "I say that the site referenced here is not in configuration database. need to check";
string s2 = "the site referenced here is not in configuration database";
if (s1.find(s2) != std::string::npos){
std::cout << " found " << std::endl;
}
string sourceString = "I say that the site referenced here is not in configuration database. need to check";
string stringToFind = "the site referenced here is not in configuration database";
sourceString.find(stringToFind);
This method call will return you the postion of type size_t
Hope this helps you
For C/C++, you can use strstr():
const char * strstr ( const char * str1, const char * str2 );
Locate substring
Returns a pointer to the first occurrence of str2 in
str1, or a null pointer if str2 is not part of str1.
If you insist on pure C++, use std::string::find:
size_t find (const std::string& str, size_t pos);
Find content in string
Searches the string for the content specified
in either str, s or c, and returns the position of the first
occurrence in the string.
Related
Can someone briefly explain how to get a character from index from String in C++.
I need to read the first 3 letters of a String and in java it would bestr.charAt(index) and I have been searching the internet for a solution for 2h now and still don't understand...
can some one please give me an example.
std::string provides operator[] to access a character by index:
https://en.cppreference.com/w/cpp/string/basic_string/operator_at
Example:
const std::string s("hello");
const char c = s[0];
// c is set to ‘h’
substr()
It returns a newly constructed string object with its value initialized to a copy of a substring of this object.
Syntax
substr(pos, pos+len)
Code
std::string str ("Test string"); //string declaration
string sub_string = str.substr(0,3);
String index starts from 0.
Best place to look would be cpluspluc.com: http://www.cplusplus.com/reference/string/string/
You may use as earlier mentioned: http://www.cplusplus.com/reference/string/string/operator[]/
std::string str ("Test string");
for (int i=0; i<str.length(); ++i)
{
std::cout << str[i];
}
Or better yet: http://www.cplusplus.com/reference/string/string/at/
std::cout << str.at(i);
which also checks for a valid position and throws an out of range exception otherwise.
Alternatively you could use http://www.cplusplus.com/reference/string/string/data/
to acces the raw data.
Or if you want to check that your string starts with a specific pattern: http://www.cplusplus.com/reference/string/string/rfind/
std::string str = "Hey Jude!";
if (str.rfind("Hey", 0) == 0) {
// match
}
Another option to obtain a single character is to use the std::string::at() member function. To obtain a substring of a certain length, use the std::string::substr member function.
In QString, contains() method works like this:
QString s = "long word";
s.contains("long"); // -> True
I would assume that QStringList works similarly, but it does not:
QStringList s;
s << "long word";
s << "longer word";
s.contains("long"); // -> False
QStringList contains searches for the exact match, which does not work like I want it to. Is there an easy way to find a part of a string in a QStringList? I could of course loop through the QStringList and use contains() there, but is there a better way?
You can use the function QStringList::filter() :
QStringList QStringList::filter(const QString &str,
Qt::CaseSensitivity cs = Qt::CaseSensitive) const
Returns a list of all the strings containing the substring str.
and check that the list returned is not empty.
In your case:
QStringList s;
s << "long word";
s << "longer word";
s.filter("long"); // returns "long word", and also "longer word" since "longer" contains "long"
I am currently trying to learn c++, and I was informed that this website is a great place to start getting involved in.
I was just wondering if it were possible to retrieve multiple characters from a string rather then repeating multiple lines of code.
string lname = "";
char l = lname.at(0);
char a = lname.at(1);
A string is essentially a char array followed by a NULL character.
The string class reference guide will help you know what you can and cannot do with a string variable. You didn't provide enough details for us to thoroughly answer your question, but if you are looking for a substring, try using the ".substr" function in the string class.
For instance:
string tempString = "Hello, my name is brw59";
// at character 7 (starts at 0) print two characters
cout << tempString.substr(7, 2); // output == "my"
For example in the following code:
char name[20] = "James Johnson";
And I want to assign all the character starting after the white space to the end of the char array, so basically the string is like the following: (not initialize it but just show the idea)
string s = "Johnson";
Therefore, essentially, the string will only accept the last name. How can I do this?
i think you want like this..
string s="";
for(int i=strlen(name)-1;i>=0;i--)
{
if(name[i]==' ')break;
else s+=name[i];
}
reverse(s.begin(),s.end());
Need to
include<algorithm>
There's always more than one way to do it - it depends on exactly what you're asking.
You could either:
search for the position of the first space, and then point a char* at one-past-that position (look up strchr in <cstring>)
split the string into a list of sub-strings, where your split character is a space (look up strtok or boost split)
std::string has a whole arsenal of functions for string manipulation, and I recommend you use those.
You can find the first whitespace character using std::string::find_first_of, and split the string from there:
char name[20] = "James Johnson";
// Convert whole name to string
std::string wholeName(name);
// Create a new string from the whole name starting from one character past the first whitespace
std::string lastName(wholeName, wholeName.find_first_of(' ') + 1);
std::cout << lastName << std::endl;
If you're worried about multiple names, you can also use std::string::find_last_of
If you're worried about the names not being separated by a space, you could use std::string::find_first_not_of and search for letters of the alphabet. The example given in the link is:
std::string str ("look for non-alphabetic characters...");
std::size_t found = str.find_first_not_of("abcdefghijklmnopqrstuvwxyz ");
if (found!=std::string::npos)
{
std::cout << "The first non-alphabetic character is " << str[found];
std::cout << " at position " << found << '\n';
}
I am having problem formatting a string which contains quotationmarks.
For example, I got this std::string: server/register?json={"id"="monkey"}
This string needs to have the four quotation marks replaced by \", because it will be used as a c_str() for another function.
How does one do this the best way on this string?
{"id"="monkey"}
EDIT: I need a solution which uses STL libraries only, preferably only with String.h. I have confirmed I need to replace " with \".
EDIT2: Nvm, found the bug in the framework
it is perfectly legal to have the '"' char in a C-string. So the short answer is that you need to do nothing. Escaping the quotes is only required when typing in the source code
std::string str("server/register?json={\"id\"=\"monkey\"}")
my_c_function(str.c_str());// Nothing to do here
However, in general if you want to replace a substring by an other, use boost string algorithms.
#include <boost/algorithm/string/replace.hpp>
#include <iostream>
int main(int, char**)
{
std::string str = "Hello world";
boost::algorithm::replace_all(str, "o", "a"); //modifies str
std::string str2 = boost::algorithm::replace_all_copy(str, "ll", "xy"); //doesn't modify str
std::cout << str << " - " << str2 << std::endl;
}
// Displays : Hella warld - Hexya warld
If you std::string contains server/register?json={"id"="monkey"}, there's no need to replace anything, as it will already be correctly formatted.
The only place you would need this is if you hard-coded the string and assigned it manually. But then, you can just replace the quotes manually.