Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

5/04/2022

Python Convert List into a space-separated string

 

refer to code

..

lst = ['I', 'am', 'a', 'humen']
strlist = ' '.join(lst)
print(strlist, type(strlist))

..

I am a humen <class 'str'>

Thank you.

6/18/2018

TCHAR to string

simple sample code

...
TCHAR buff[1024];
///
///..something to do
///
wstring test(&buff[0]); //convert to wstring
string strFolder(test.begin(), test.end()); //and convert to string.
...

5/25/2018

print each character in string and some useful functions

This is example source code for how to print and access each character in string.
And using this function, I made some useful functions.
Thank you.


...
void printAndCountAllCh(string str)
{
    int count = 0;
    for (std::string::iterator it = str.begin(); it != str.end(); ++it) {
        std::cout << count << ": " << *it << endl;
        count++;
    }
}

string changeAtoB(string str, char A, char B)
{
    int count = 0;
    for (std::string::iterator it = str.begin(); it != str.end(); ++it) {
        if (string(1, *it) == string(1, A))
        {
            *it = B;
        }
    }

    return str;
}

string deleteA(string str, char A)
{
    string rstr;
    int count = 0;
    for (std::string::iterator it = str.begin(); it != str.end(); ++it) {
        if (string(1, *it) != string(1, A))
        {
            rstr.push_back(*it);
        }
    }

    return rstr;
}


int countNotCH(string str, char CH)
{
    int count = 0;
    for (std::string::iterator it = str.begin(); it != str.end(); ++it) {
        if (string(1, *it) != string(1, CH))
            count++;
    }
    return count;
}

int main()
{

    string str = "A_B CD_EF GH_I";
    int count;

    printAndCountAllCh(str); //print each char and count
    std::cout << endl;

    string abc = changeAtoB(str, ' ', '_'); //A_B CD_EF GH_I -> A_B_CD_EF_GH_I
    std::cout << abc << endl << endl;

    string bbb = deleteA(str, ' '); //A_B CD_EF GH_I -> A_BCD_EFGH_I
    std::cout << bbb << endl << endl;

    count = countNotCH(str, '_'); //A_B CD_EF GH_I -> 11
    std::cout << count << endl << endl;

    return 0;
}
...




3/16/2018

simple tip: int to string in stl

memo for me.. ^^

for(int i=0; i<10; ++i)
     std::string index1("string_" + std::to_string(i) + ".jpg");



6/15/2017

small tip : Convert char * to LPCTSTR

CA2W(str)

for example

char str[100];
sprintf(str, "str_%d", 100);
CString A = CA2W(str);

^^

1/02/2017

CString to string


..
string CstringToString(CString str)
{
    CT2CA pszConvertedAnsiString(str);
    std::string s(pszConvertedAnsiString);

    return s;
}
..

12/08/2016

convert char* to LPWSTR

refer to below example


char text[] = "something";
wchar_t wtext[20];
mbstowcs(wtext, text, strlen(text)+1);//Plus null
LPWSTR ptr = wtext;

12/07/2016

convert tchar * to char * / char * to tchar*

tchar* -> char*

"Tch" is tchar*
"pStrTch" is char*


TCHAR Tch[MAX_PATH + 1];
int lenOftch = (wcslen(Tch) + 1) * 2;
char * pStrTch = (char*)malloc(sizeof(char)*MAX_PATH);

//tchar* to char*
WideCharToMultiByte(CP_UTF8, 0, Tch, -1, pStrTch, lenOftch, NULL, NULL);
//


char* -> tchar*


char charBuff[]="test";
TCHAR szUniCode[256]={0,};
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, charBuff, strlen(charBuff), szUniCode, 256);

3/13/2014

C, C++, The meaning of LPSTR, LPCSTR, LPTSTR, LPCTSTR, LPWSTR

LP is Long Pointer.
C is constant.
STR is String.

LPSTR = long pointer string = char*
LPCSTR = long pointer constant string = const char*



W means Wide char and unicode.

LPWSTR = long porinter wide string = w_char*
LPCWSTR = long porinter constant wide string = const w_char*


T is ..

LPCTSTR = long pointer constant t_string = const tchar*