Convert all instances of "\" to "\\" in System::String^ and convert to char* - string

I have an openFileDialog that returns a filename. I want to save that filename as a char* so that I can open it later using fstream. To do this I'll need to replace all instances of \ with \\ in the string.
My tactic for doing this is to split the System::String^ on every instance of \, and then join all the elements of the resulting System::String^ array back together with \\ separating them.
This is the function that I have written, but it seems to return a blank string even when I pass a String^ that contains \.
private: const char* getCharPointer(String^ name){
array<String^>^ words;
String^ delimStr = "\\";
array<Char>^ delimiter = delimStr->ToCharArray( );
String^ replaceDelim = "\\\\";
words = name->Split(delimiter);
String^ tidiedName = String::Join( replaceDelim, words );
label1->Text = tidiedName;
std::string newname=msclr::interop::marshal_as< std::string >( tidiedName);
const char* name_cstr = newname.c_str();
return name_cstr;
}
I am a newcomer to Visual C++ and Windows in general so I would appreciate any pointers on this. It doesn't help that IntelliSense doesn't seem to work with Visual Studio 2010.

Intellisense is not provided in VC++ and It seems it will never be added into VC++.
Anyway, you can use, instead of Join, Replace.
You can see here the reference.
Look at this simple program [ I'm using a Windows Form Application and this code is under a button event handler].
String^ a = "Hello\\World";
String^ b = a->Replace("\\", "\\\\"); //Replace \ with \\
MessageBox::Show(a + " -> " + b);
if you run it, you will get this output.
Hello\World -> Hello\\World
I hope this one is what you need for your code!

Related

C++ How to parse math problem in string using TinyExpr?

First I need to detect if message from player contains math problem
like I get 2 messages from him
char* message1 = "Please solve this math problem";
char* message2 = "How much is 1 + 2 ?";
These messages are not static, player can say different messages.
I need to process message1 and message2
I need to detect math problem and cut it like see message2 and I need it to be "1+2"
char* mathProblemFromMessage = "1+2"; // <- I need to get this from message also detect if message contains any math problem before
double answer = te_interp(mathProblemFromMessage, 0);
printf("Answer is %f\n", answer);
my current code
std::string msg = Message;
if (func.contains_math_operators(msg.c_str()) && func.contains_number(msg.c_str()))
{
double answer = te_interp(Message, 0);
}
I think it fails and always results 0 because msg is "Player: 1+1" I need to cut math problem from it, I dont know how to do it...a
Find a position of the first digit in that string and get a substring from that point:
std::size_t found = msg.find_first_of("0123456789");
if(found != std::string::npos)
{
std::string sub = msg.substr(found);
double answer = te_interp(sub, 0);
}
If that also fails, try to trim invalid tail.
You may want to extend that find_first_of above to include open paren (, unary minus - or some supported function names (I am not familiar with TinyExpr)

C++ Using System::String::Split

I am trying to split a System::String at ":" using System::String::Split in my C++ code. The string to be split is called "responseString". It is a System::String. I have:
char id[] = { ':' };
return responseString->Split(id);
However, it errors at the "->" saying that no instance of the overloaded function matches the argument list. I checked the MSDN documentation, and see no information on doing this in C++.
I also tried the following, with the same results:
System::Char id[] = { ':' };
return responseString->Split(id);
In the documentation it shows the following example, but I do not know what to do with that:
array<String^>^ Split(
... array<wchar_t>^ separator
)
Any help would be appreciated!
Symbol delimiter can be either of type wchar_t, either String ^, as well as their array.
Here's a quick example:
String ^str="String:need:split;this";
array<wchar_t> ^id = { ':' ,';'};
array<String^> ^StringArray =str->Split(':');
array<String^> ^StringArray2 =str->Split(id);
for each(String^ temp in StringArray)
Console::WriteLine(temp);
Console::WriteLine();
for each(String^ temp in StringArray2)
Console::WriteLine(temp);

How to remove accents / diacritic marks from a string in Qt?

How to remove diacritic marks from a string in Qt. For example, this:
QString test = QString::fromUtf8("éçàÖœ");
qDebug() << StringUtil::removeAccents(test);
should output:
ecaOoe
There is not straighforward, built-in solution in Qt. A simple solution, which should work in most cases, is to loop through the string and replace each character by their equivalent:
QString StringUtil::diacriticLetters_;
QStringList StringUtil::noDiacriticLetters_;
QString StringUtil::removeAccents(QString s) {
if (diacriticLetters_.isEmpty()) {
diacriticLetters_ = QString::fromUtf8("ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ");
noDiacriticLetters_ << "S"<<"OE"<<"Z"<<"s"<<"oe"<<"z"<<"Y"<<"Y"<<"u"<<"A"<<"A"<<"A"<<"A"<<"A"<<"A"<<"AE"<<"C"<<"E"<<"E"<<"E"<<"E"<<"I"<<"I"<<"I"<<"I"<<"D"<<"N"<<"O"<<"O"<<"O"<<"O"<<"O"<<"O"<<"U"<<"U"<<"U"<<"U"<<"Y"<<"s"<<"a"<<"a"<<"a"<<"a"<<"a"<<"a"<<"ae"<<"c"<<"e"<<"e"<<"e"<<"e"<<"i"<<"i"<<"i"<<"i"<<"o"<<"n"<<"o"<<"o"<<"o"<<"o"<<"o"<<"o"<<"u"<<"u"<<"u"<<"u"<<"y"<<"y";
}
QString output = "";
for (int i = 0; i < s.length(); i++) {
QChar c = s[i];
int dIndex = diacriticLetters_.indexOf(c);
if (dIndex < 0) {
output.append(c);
} else {
QString replacement = noDiacriticLetters_[dIndex];
output.append(replacement);
}
}
return output;
}
Note that noDiacriticLetters_ needs to be a QStringList since some characters with diacritic marks can match to two single characters. For example œ => oe
Your question is a bit misleading. You seem to want to do more than merely remove diacritical marks (œ is a ligature letter without diacritics). I guess you want to turn any Unicode string into a roughly corresponding ASCII string?
For diacritics, you could perform a decomposing Unicode normalization (NFD or NFKD, depending on your specific needs) and then remove all characters of the "Mark" categories (QChar::Mark_NonSpacing, QChar::Mark_SpacingCombining and QChar::Mark_Enclosing).
For everything else (e.g. œ), I don't know of a generic solution. Create a look-up table with all your desired replacements and then search and replace (see Laurent's answer).
A partial solution is to use QString::normalized, than remove the special characters.
QString test = QString::fromUtf8("éçàÖœ");
QString stringNormalized = test.normalized (QString::NormalizationForm_KD);
stringNormalized.remove(QRegExp("[^a-zA-Z\\s]"));
This is however a partial solution because it will not convert "œ" into "oe".
There is crude way to partial solve your problem (just accents, but not ligatures such as "oe").
QString title=QString::fromUtf8("éçàÖ");
qDebug("%s\n", title.toLocal8Bit().data());

How do i random a number in a string?

I´m trying to set a random number in a string, but idk how i can let the program know that i want the random number and not the letter n.
Im using visual studio 2008 , Windows Forms C++
System::Drawing::Font ^Fuente = gcnew System::Drawing::Font("Arial Black",50);
System::Random ^r = gcnew System::Random(System::DateTime::Now.Ticks);
char n=r->Next(1,100);
buffer->Graphics->DrawString("n",Fuente,System::Drawing::Brushes::WhiteSmoke,50,50);,50);
System::Drawing::Font ^Fuente = gcnew System::Drawing::Font("Arial Black",50);
System::Random ^r = gcnew System::Random(System::DateTime::Now.Ticks);
int n=r->Next(1,100);
buffer->Graphics->DrawString(n.ToString(), Fuente, System::Drawing::Brushes::WhiteSmoke,50,50);,50);
Might be what you are after
You're using quotes when drawing string - that's a string literal.
You should convert your number to a string using stdlib.h itoa function.
char number[3];
int n = r->Next(1,100);
itoa(n, number, 10); //number to convert, string to save value in, base
buffer->Graphics->DrawString(number,Fuente,System::Drawing::Brushes::WhiteSmoke,50,50);,50);

How to Copying a folder in VC++?

How to copy a folder from one drive to other drive in VC++ ...?
I have come this far
String^ SourcePath = Directory::GetCurrentDirectory();
String^ DestinationPath = "c:\\Test";
CString s(SourcePath) ;
CString d(DestinationPath);
Directory::CreateDirectory(DestinationPath);
SHFILEOPSTRUCT* pFileStruct = new SHFILEOPSTRUCT;
ZeroMemory(pFileStruct, sizeof(SHFILEOPSTRUCT));
pFileStruct->hwnd = NULL;
pFileStruct->wFunc = FO_COPY;
pFileStruct->pFrom = (LPCWSTR)s;//"D:\test_documents\test1.doc";
pFileStruct->pTo = (LPCWSTR)d;
pFileStruct->fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR ;
bool i = pFileStruct->fAnyOperationsAborted ;
int status = SHFileOperation(pFileStruct);
if(status == 0)
{
return true;
}
return false;
the status is showing 2 instead of zero , can some one tell me why..?
Usually a String^ points to a managed string object. The SHFILOPSSTRUCT must befilled with pointers to unmanaged wchar_t. So you must pin the strings and convert. You tried to use the CString class as conversion helper.
Use the PtrToStringChars instead to get valid strings in pTo and pFrom:
http://msdn.microsoft.com/en-us/library/d1ae6tz5(VS.80).aspx
The read of the fAnyOperationsAborted member is not required for the operation.

Resources