CString Mid()
-
I have a CString object loaded with a string like "This is the first section$This is the second section$This is the end" so each section separated by '$'. I try to load the second section as csAnsprech = csComboBoxText.Mid( csComboBoxText.Find( '$') +1, csComboBoxText.ReverseFind( '$')); but this retrieves the second and third section, nut just the second one. What´s wrong here ?
-
I have a CString object loaded with a string like "This is the first section$This is the second section$This is the end" so each section separated by '$'. I try to load the second section as csAnsprech = csComboBoxText.Mid( csComboBoxText.Find( '$') +1, csComboBoxText.ReverseFind( '$')); but this retrieves the second and third section, nut just the second one. What´s wrong here ?
-
I have a CString object loaded with a string like "This is the first section$This is the second section$This is the end" so each section separated by '$'. I try to load the second section as csAnsprech = csComboBoxText.Mid( csComboBoxText.Find( '$') +1, csComboBoxText.ReverseFind( '$')); but this retrieves the second and third section, nut just the second one. What´s wrong here ?
Hello RadioOpa, try this:
int i =0; while(i < csComboBoxText.GetLength()) { if(csComboBoxText.GetAt(i) == '$') // if we found the first separator in the string { i++; // move one place to skip the separator while(csComboBoxText.GetAt(i) != '$') // now copy all characters in your string, while not found the next separator { csAnsprech += csComboBoxText.GetAt(i); i++; // move to the next character } } i++;// move to the next character }
i hope that helps you! regards break; P.S. that sample from nave ist very good, i want just to show you another way without Find-Functions to extract a part of string -- modified at 2:55 Tuesday 21st March, 2006 -
Hello RadioOpa, try this:
int i =0; while(i < csComboBoxText.GetLength()) { if(csComboBoxText.GetAt(i) == '$') // if we found the first separator in the string { i++; // move one place to skip the separator while(csComboBoxText.GetAt(i) != '$') // now copy all characters in your string, while not found the next separator { csAnsprech += csComboBoxText.GetAt(i); i++; // move to the next character } } i++;// move to the next character }
i hope that helps you! regards break; P.S. that sample from nave ist very good, i want just to show you another way without Find-Functions to extract a part of string -- modified at 2:55 Tuesday 21st March, 2006Many thanks, both ways solving the problem are excellent. I changed code as follows: iFirstSection = csComboBoxText.Find( '$'); // Find first '$' iSecondSection = csComboBoxText.Find( '$', iFirstSection +1); // Find second '$' csFirma = csComboBoxText.Mid( 0, iFirstSection); // Get first string section csFirma.TrimRight( ' '); csOrt = csComboBoxText.Mid( iFirstSection +1, iSecondSection - iFirstSection); // Get second csOrt.TrimRight( ' '); csFirmaID = csComboBoxText.Mid( iSecondSection +1); // Get last section
-
I have a CString object loaded with a string like "This is the first section$This is the second section$This is the end" so each section separated by '$'. I try to load the second section as csAnsprech = csComboBoxText.Mid( csComboBoxText.Find( '$') +1, csComboBoxText.ReverseFind( '$')); but this retrieves the second and third section, nut just the second one. What´s wrong here ?
The second param to Mid() is the # of characters, not the ending index. You can use AfxExtractSubString() to pick out the tokens. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ