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.
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.
'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];
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).
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With