NetInfo
NetInfo

Reputation: 597

How to define a string variable in C++

I have been working with VB for a while now. Now I'm giving C++ a shot, i have came across strings, i cant seem to find a way to declare a string.

For example in VB:

Dim Something As String = "Some text"

Or

Dim Something As String = ListBox1.SelectedItem

Whats the equivalent to the code above in C++ ?

Any help is appreciated.

Upvotes: 17

Views: 187209

Answers (4)

argue2000
argue2000

Reputation: 31

In C++ you can declare a string like this:

#include <string>

using namespace std;

int main()
{
    string str1("argue2000"); //define a string and Initialize str1 with "argue2000"    
    string str2 = "argue2000"; // define a string and assign str2 with "argue2000"
    string str3;  //just declare a string, it has no value
    return 1;
}

Upvotes: 3

Bojan Komazec
Bojan Komazec

Reputation: 9526

Preferred string type in C++ is string, defined in namespace std, in header <string> and you can initialize it like this for example:

#include <string>

int main()
{
   std::string str1("Some text");
   std::string str2 = "Some text";
}

More about it you can find here and here.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

C++ supplies a string class that can be used like this:

#include <string>
#include <iostream>

int main() {
    std::string Something = "Some text";
    std::cout << Something << std::endl;
}

Upvotes: 29

Ben Cottrell
Ben Cottrell

Reputation: 6110

using the standard <string> header

std::string Something = "Some Text";

http://www.dreamincode.net/forums/topic/42209-c-strings/

Upvotes: 3

Related Questions