Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ String Declaring

Tags:

c++

string

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.

like image 579
NetInfo Avatar asked Apr 18 '12 22:04

NetInfo


People also ask

How do you declare a string in C?

Declaring a string is as simple as declaring a one-dimensional array. Below is the basic syntax for declaring a string. char str_name[size]; In the above syntax str_name is any name given to the string variable and size is used to define the length of the string, i.e the number of characters strings will store.

How strings are declared and initialized in C?

'C' language does not directly support string as a data type. Hence, to display a String in C, you need to make use of a character array. The general syntax for declaring a variable as a String in C is as follows, char string_variable_name [array_size];

How do you declare a string and initialize it?

To declare and initialize a string variable: Type string str where str is the name of the variable to hold the string. Type ="My String" where "My String" is the string you wish to store in the string variable declared in step 1. Type ; (a semicolon) to end the statement (Figure 4.8).


1 Answers

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;
}
like image 97
Sergey Kalinichenko Avatar answered Oct 01 '22 00:10

Sergey Kalinichenko