I have problem with getting out a character or characters of a string in C++.
It was easy in python, for example I could take the first block of a string like this:
x = "Hello Word"
p = x[0]
now the H character would save in p.
As mentioned, surely you mean a char (character?)
std::string x = "Hello Word";
char p = x[0]; //p now contains 'H'
See http://en.cppreference.com/w/cpp/string/basic_string for more detail (thanks for suggestion of link)
Related
Let's imagine I have
String x = "hello there";
So I can print it from index e.g. 1 as:
Serial.println(x.substring(1));
ello there
I wanna do the same with
char x[] = "hello there";
Any ideas? (Except using loops to print char by char)
You can use the & operator to get the string after the desired index like this:
Serial.println(&x[1]);
I wrote a simple program to print a unicode smile emoji. Unfortunately, something else is printed. Does anyone know what the problem with this code is? Thanks
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string str = u8"\u1F600";
cout << str << endl;
return 0;
}
Compilation and output:
g++ -pedantic -Wall test109.cc && ./a.out
ὠ0
\u escape sequences have the format \u#### (i.e. exactly 4 hex digits). You need \U########:
auto str = u8"\U0001F600";
Or, encoding the UTF8 bytes separately:
auto str2 = u8"\xf0\x9f\x98\x80";
That works.
The \u escape sequence is limited to 4 hex digits max, so "\u1F600" is parsed as two separate characters \u1F60 (ὠ) and 0, which is exactly what you are seeing in your console output.
Codepoint U+1F60 GREEK SMALL LETTER OMEGA WITH PSILI is very different than codepoint U+1F600 GRINNING FACE.
For what you are trying, you need to use the \U escape instead, which allows up to 8 hex digits:
string str = u8"\U0001F600";
Alternatively, you can use one of these instead:
string str = u8"\xF0\x9F\x98\x80"; // UTF-8 codeunits in hex format
string str = u8"\360\237\230\200"; // UTF-8 codeunits in octal format
string str = u8"😀"; // if your compiler/editor allows this
You can use any of the following which works for you.
string str = "\u263A"; // --> ☺
//string str = u8"\xe2\x98\xba"; --> ☺
//string str = u8"\U0001F600"; --> 😀
//string str = u8"😀"; --> 😀
//string str = "\342\230\272" --> ☺
cout << str << endl;
what is char ctemp = ' '; and string stemp = ""; means? when they put ' ' and " " inside without writing anything inside? Help please! Will appreciate who answer it.
Single quotes (') indicate a character literal: a single character. Double quotes (") denote a string literal, i.e: an array of characters.
' ' is a single space character, while " " is a single space character followed by a null terminator, as is customary for C-style strings.
Character literals are directly assignable to char variables.
The type of a string literal is const char[N], where N is the length of the literal, including the null terminator. In C and C++, a static array decays to (is implicitly convertible to) a pointer to the first element, and std::string is constructible from a const char * pointer (see constructor (5)), which in C usually means a pointer to an array of characters terminated by a null terminator.
The char ctemp = ' ' will put the value ' ' (32 in ASCII decimal) inside the ctemp variable.
The string stemp = ""; will create an empty string in stemp.
Here
char ctemp = ' ';
you are assigning a whitespace character ' ' to ctemp.
Here
string stemp = "";
the initializer "" creates a empty string.
' ' is the space character. "" is an empty string. " " is a string that contains only the space character.
Note that a statement like string stemp = "" implicitly invokes the string(char const *) constructor to create a new string instance from a char const * pointer.
the first one means a "whitespace" like when you write something and need to divide the words with the space key. That empty space is still part of the string and so you can say your char is only that empty space.
The second one is of type string but it is even less than a white space. It is a completely empty string.
string is array(collection) of char
ctemp = ' '
mean whitespace character
stemp = ""
mean empty string no character in string
you can put ' ' to char variable.
you can put " " to array of char.
In C++ the single quote is used to identify the single character, and double quotes are used for string literals. A string literal “x” is a string, it is containing character ‘x’ and a null terminator ‘\0’. So “x” is two-character array in this case.
Some Examples:
string s = "" ; => empty string
char s =' ' ; => space (you should have only one character inside the single quotes)
string s = " " ; => space followed by '\0' character (two character array)
Whitespace character and empty string. You can see the string as a sequence of characters, but they are two different types
I have the following string:
{'output',{'variable','VGRG_Pos_Var1/Parameters/D_foo'},'date',734704.60904050921}
I would like to verify the format of the string that the word 'variable' is the second word and i would like to retrive the string after the last '/' in the 3rd string (In this example 'D_foo').
how could i verify this and retrive the sting i search?
I tried the following:
regexp(str,'{''\w+'',{''variable'',''([(a-z)|(A-Z)|/|_])+')
without success
REMARK
The string to analysis is not splited after the komma, it is only due to length of the string.
EDIT
my string is:
'{''output'',{''variable'',''VGRG_Pos_Var1/Parameters/D_foo''},''date'',734704.60904050921}';
and not a cell, which could be understood. I added the sybol ' at the start and end of the string to symbolizied that it is a string.
I realise that you mention using regexp in the question, but I'm not sure if this is a requirement? If other solutions are acceptable you could try this:
str='{''output'',{''variable'',''VGRG_Pos_Var1/Parameters/D_foo''},''date'',734704.60904050921}';
parts1=textscan( str, '%s','delimiter',{',','{','}'},'MultipleDelimsAsOne',1);
parts2=textscan( parts1{1}{3}, '%s','delimiter',{'/',''''},'MultipleDelimsAsOne',1);
string=parts2{1}{end}
match=strcmp(parts1{1}{2},'variable')
To answer the first part of your question, you can write this:
str = {'output',{'variable','VGRG_Pos_Var1/Parameters/D_foo'},'date',734704.60904050921};
temp = str(2); %this holds the cell containing the two strings
if cmpstr(temp{1}(1), 'variable')
%do stuff
end
For the second part you can do this:
str = {'output',{'variable','VGRG_Pos_Var1/Parameters/D_foo'},'date',734704.60904050921};
temp = str(2); %like before, this contains the cell
temp = temp{1}(2); %this picks out the second string in the cell
temp = char(temp); %turns the item from a cell to a string
res = strsplit(temp, '/'); %splits the string where '/' are found, res is an array of strings
string = res(3); %assuming there will always be just 2 '/'s.
int main()
{
char* a = " 'Fools\' day' ";
char* b[64];
sscanf(a, " '%[^']s ", b);
printf ("%s", b);
}
--> puts "Fools" in b
Obviously, I want to have "Fools' day" in b. Can I tell sscanf() not to consider escaped apostrophes as the end of the character sequence?
Thanks!
No. Those functions just read plain old characters. They don't interpret the contents according to any escaping rules because there's nothing to escape from — quotation marks, apostrophes, and backslashes aren't special in the input string.
You'll have to use something else to parse your string. You can write a little state machine to read the string one character at a time, keeping track of whether the previous character was a backslash. (Don't just scan to the next apostrophe and then look one character backward; if you're allowed to escape backslashes as well as apostrophes, then you could end up re-scanning all the way back to the start of the string to see whether you have an odd or even number of escape characters. Always parse strings forward, not backward.)
Replace
char* a = " 'Fools\' day' ";
with
char* a = " 'Fools' day' ";
The ' character isn't special inside a C string (although it is special within a single char). So there is not need to escape it.
Also, if all you want is "Fools' day", why put the extra 's at the start and end? Maybe you are confusing C strings with those in some other language?
Edit:
As Rob Kennedy's comment says, I was assuming you are supplying the string yourself. Otherwise, see Rob's answer.
Why on earth would you write such a thing, instead of using std::string? Since your question is tagged C++.
int main(int argc, char* argv[])
{
std::string a = " 'Fools' day' ";
std::string b(a.begin() + 2, std::find(a.begin() + 2, a.end(), ' '));
std::cout << b;
std::cin.get();
}
Edit: Oh wait a second, you want to read a string within a string? Just use escaped double quotes, e.g.
int main(int argc, char* argv[]) {
std::string a = " \"Fool's day\" ";
auto it = std::find(a.begin(), a.end(), '"');
std::string b(it, std::find(it, a.end(), '"');
std::cout << b;
}
If the user put the string in, they won't have to escape single quotes, although they would have to escape double quotes, and you'd have to make your own system for that.