Assuming:
std::string ToShow,NumStr;
The following displays "This is 19 ch00":
ToShow = "This is nineteen ch";
ToShow.resize(ToShow.length()+0);
NumStr = "00";
ToShow += NumStr;
mvaddstr(15,0,ToShow.c_str());
And the following displays "This is 19 ch ":
ToShow = "This is nineteen ch";
ToShow.resize(ToShow.length()+1);
NumStr = "0";
ToShow += NumStr;
mvaddstr(16,0,ToShow.c_str());
In the second case, operator+= isn't adding the string "0" to the end of ToShow. Does anyone know why?
My guess is:
You don't specify the value to resize with, so after ToShow.Resize(ToShow.length()+1) your string looks like:
"This is nineteen ch\0"
And after += NumStr:
"This is nineteen ch\00"
which, after calling c_str, gets trimmed to the first \0 and looks like:
"This is nineteen ch"
(C strings are null-terminated, std::strings aren't)
Try calling .resize(someLength, ' ') instead.
Related
I am trying to make a (assembly) parser which uses a string as a guide for how to cut the text to get the tokens I want.
string s = "$t4,";
string guide = "$!,$!,$!";
int i = 1;
string test =s.substr(0, s.find_first_of(" ,.\t"+to_string(guide[i+1]) ));
cout << test << "\n";
if s = "$t4" then test = "$t"
what I am expecting it to do is test to be "$t4", this works for every other $tX except for specifically the number 4 even though it's not in the (" ,.\t"+to_string(guide[i+1])) string
s.find_first_of(" ,.\t" + std::to_string(guide[i + 1]))
Assuming ASCII, that string will be:
,.\t44
44 is the ASCII value of the , in guide[i + 1].
The first character in "$t4," that it'll find is 4 at position 2, and you then create a substring from 0 and length 2, that is $t.
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;
I have a situation where I want to get separator char from the given string like as below :-
String str1 = "saurabh|om|anurag|abhishek|jitendra"
String str2 = "amit,ankur,sumit,aniket,suheel"
String str3 = "aj-kumar-manav-lalit-gaurav"
-------
In above strings I want to get separator char as :-
String separatorStr1 = "|"
String separatorStr2 = ","
String separatorStr3 = "-"
Note :- separator char always will be non-alphabetical in string
Is there any way to achieve this.
Using groovy regexp and find ([^\w] is any non-alphanumeric character)
def getSeparator = { str ->
str.find(~/[^\w]/)
}
String str1 = "saurabh|om|anurag|abhishek|jitendra"
String str2 = "amit,ankur,sumit,aniket,suheel"
String str3 = "aj-kumar-manav-lalit-gaurav"
assert getSeparator(str1) == '|'
assert getSeparator(str2) == ','
assert getSeparator(str3) == '-'
Why is a - separator of str3? It could be a as well.
Assuming separator must be non-alphabetical loop through characters and look for first non-alphabetical character.
In future questions try to avoid other users guessing what you mean - try to define the subject of a topic.
By xenteros suggestion I have achieved this by following way :-
String str1 = "saurabh|om|anurag|abhishek|jitendra"
String str2 = "amit,ankur,sumit,aniket,suheel"
String str3 = "aj-kumar-manav-lalit-gaurav"
String separatorStr1 = str1.toCharArray().find { !Character.isLetterOrDigit(it) }
String separatorStr2 = str2.toCharArray().find { !Character.isLetterOrDigit(it) }
String separatorStr3 = str3.toCharArray().find { !Character.isLetterOrDigit(it) }
assert separatorStr1 == '|'
assert separatorStr2 == ','
assert separatorStr3 == '-'
//build a greeting
const string greeting = "Hello, " + name + "!"; <-- nsme is a string entered in cmd line
//build 2nd and fourth lines of the output
const string spaces(greeting.size()," "); <-- getting red line here
const string second = "* " + spaces + " *";
//build the first and fifth lines
const string first(second.size(), "*"); <-- getting red line here
I understand what the code is trying to do - ie: set the size - but I'm not sure why this isn't working.
Intellisense had 'size' in the options list after I hit the '.' following greeting and second so I expected this to work. Using VS2013 pro.
const string spaces(greeting.size()," ");
std::string does not have a constructor that can take those arguments.
What do you expect it to do? If you want a string with N copies of a single character you need to pass it a single character, not an array of characters i.e.
const string spaces(greeting.size(), ' ');
how to get the 1st name. here is the sample of data.. first name here is Owen, Florencio. I need to read and get the value frm whitespace to ; ??
Owen;Grzegorek;Howard Miller Co;15410 Minnetonka Industrial Rd;Minnetonka;Hennepin;MN;55345;952-939-2973;952-939-4663;owen#grzegorek.com;http://www.owengrzegorek.com
Florencio;Hollberg;Hellenic Museum & Cultural Ctr;2211 Kenmere Ave;Burbank;Los
Use string::find to find the first instance of semi-colon, and do a string::substr.
string str = "Florencio;Hollberg;Hellenic Museum & Cultural Ctr;2211 Kenmere Ave;Burbank;Los";
std::size_t pos = str.find(";");
str = str.substr(0, pos);
cout << str << endl;
Output:
Florencio
Of course, you have to modify the code to suit your needs.