Hello,

I'm wanting to know whats in the string and where: I've got this code

string s1="HelloWorld";

if(s1[5] = "W")

now then if i want to know if World is in the string. I've tried doing:

the if....

but it errors:


: error C2446: '==' : no conversion from 'char *' to 'const char *(__thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::*'
There is no context in which this conversion is possible

: error C2040: '==' : 'const char *(__thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::*' differs in levels of indirection from 'char [2]'
Error executing cl.exe.

Dani AI

Generated

— the compiler errors come from two separate issues: using the assignment operator in an if and mixing a single character access with a string literal. The clean ways to check are (a) test a single character safely, (b) search for a substring, or (c) compare a slice of the string. Examples below show safe checks and basic pitfalls to avoid.

// safe single-character check (bounds-checked)
std::string text = "ExampleWorld";
size_t i = 5;             // zero-based index
if (i < text.size() && text[i] == 'W') {
    // found 'W' at position i
}
// substring search using find (preferred for presence)
if (text.find("World") != std::string::npos) {
    // "World" appears somewhere in text
}
// exact match at a known position without extra allocation
if (text.compare(7, 5, "World") == 0) {
    // "World" starts at position 7
}

Notes and troubleshooting tips:

  • Indexes are zero-based; the first character is at index 0.
  • Use single quotes for characters ('W') and double quotes for C-style strings ("W"). Mixing them causes type-mismatch errors (as pointed out).
  • Avoid using assignment (=) inside an if when you meant comparison (==).
  • Prefer a bounds check (i < s.size()) or s.at(i) (throws on out-of-range) to prevent undefined behavior.
  • std::string::npos (note the double colon) is the value returned by find when the substring is not found — compare to that, not to -1.
  • For performance-sensitive code in C++17+, consider std::string_view to avoid allocations when slicing.

Thanks to the points raised by , , , and — the above combines their correct suggestions and adds safe, idiomatic patterns you can apply immediately.

Recommended Answers

All 4 Replies

Hello,

I'm wanting to know whats in the string and where: I've got this code

string s1="HelloWorld";

if(s1[5] = "W")

now then if i want to know if World is in the string. I've tried doing:

the if....

but it errors:

#include <iostream>
#include <string>

int main()
{
	std::string ex = "Hello World";

	if (ex[6] == 'W')
		std::cout << "W exists in the string." << std::endl;
	std::cout << "Press enter to exit." << std::endl;
	std::cin.get();

	return 0;
}

My book of C++ programming says that, "The relational operators can be applied to variables of type string. Variables of the type string are compared character-by-character, starting with the first character" (C++ Programming: From Problem Analysis To Program Design, Malik, Page 148)
Randy

std::string s1="HelloWorld";
if(s1.find("World",0) != std::string.npos) {
  printf("found");
}
else {
  printf("not there");
}

My book of C++ programming says that, "The relational operators can be applied to variables of type string. Variables of the type string are compared character-by-character, starting with the first character" (C++ Programming: From Problem Analysis To Program Design, Malik, Page 148)
Randy

Correct, however, this isn't what the code is doing - s[5] returns a char and not a std::string.

"W" with double quotes is of type const char* (a pointer to the first element of a string literal)

if you were to do s[5] == 'W' then this comparison would be ok. The difference being that 'W' with single quotes is also of type char.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.