Substring with start and end

Hi folks, I'm trying to understand a little bit better the string and in this case substring set within C and C++.

What I need is to retrieve the Text typed into a TEdit Box, for example an email, and get the substring from the beggining to the "@" and display it on another TEdit Box, and everything after the "@" in a third TEdit Box when I click on a Button. So it would separate the "user", the "Domain" and the "@".

I tried but since the value retrieved from the TEdit Box is Unicode the string and substring that I tried didn't work.

By the way I'm using XE6 C++ Builder.

Any help owuld be much appreciated. Cheers!
Rodric MacOliverResearcherAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sarabandeCommented:
TEdit Box is Unicode the string and substring that I tried didn't work.

you may try the following:

#include <string>
...
std::wstring strEdit = strYourEditString;

std::wstring strPart1, strPart2;

size_t pos = strEdit.find(L'@');
if (pos != std::wstring::npos)
{
       strPart1 = strEdit.substr(0, pos);
       strPart2 = strEdit.substr(pos+1);
}
else
{
     strPart1 = strEdit;
}

Open in new window

}
you could assign a wstring to variables of your string class like

yourstring = wstr.c_str();

Open in new window


Sara
0
ZoppoCommented:
I agree with Sara, but I would suggest do a more convenient parsing to filter out wrong mail addresses, i.e.
bool ParseMail( const std::wstring& address, std::wstring& name, std::wstring& domain )
{
	if ( address.find_first_of( L"{[()]}\\/|<>:;,.\t " ) != std::wstring::npos )
	{
		// fail if one of the above characters appear (this maybe not a complete list, to
		// be honest I don't know all characters not allowd in a mail address from the scratch
		return false;
	}

	std::wstring::size_type pos = address.find( L"@" );

	if ( pos == std::wstring::npos )
	{
		// '@' not found
		return false;
	}

	if ( pos == 0 || pos == address.length() - 1 )
	{
		// found '@' at start or end
		return false;
	}

	if ( address.find( L"@", pos + 1 ) != std::wstring::npos )
	{
		// more than one '@' found
		return false;
	}

	name = address.substr( 0, pos );
	domain = address.substr( pos + 1 );

	return true;
}

Open in new window

This can be used like this:

   std::wstring name, domain;
   if ( ParseMail( strYourEditString, name, domain ) == false )
   {
    // handle error
   }
  else
  {
   // use the strings name and domain as you need
  }

Hope that helps,

ZOPPO
0
Subrat (C++ windows/Linux)Software EngineerCommented:
TEdit->Name returns UnicodeString. So you have to convert it to std:string/wstring, then pass that to the function provided by ZOPPO and sara.Use the following function for conversion.

// Convert wstring to UnicodeString
String ToUniStr(const std::wstring &str)
{
   char *cBuf = new char[str.length()+1];
   memset( cBuf, 0, str.length()+1);

   wcstombs(cBuf, str.c_str(), str.length());

   String aBuf = cBuf;
   delete[] cBuf;
   return aBuf;
}

In my opinion, use sara's solution in your first attempt though you are in learning stage.
But latter use ZOPPO's solution which is more robost.

Please let us know if you need any clarifications on the above codes.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Rodric MacOliverResearcherAuthor Commented:
Unfortunately neither work, still give me the error for Unicode and String...
I'm still trying so any other help would be appreciated...
Cheers
0
sarabandeCommented:
can you post the code that doesn't work and a pointer to the statement which doesn't compile?

if using class UnicodeString it has member function c_str which returns const wchar_t * what is a pointer to a wide character string. then the following code should work:

#include <string>
...
std::wstring strEdit = strYourUnicodeString.c_str();

std::wstring strPart1, strPart2;

size_t pos = strEdit.find(L'@');
if (pos != std::wstring::npos)
{
       strPart1 = strEdit.substr(0, pos);
       strPart2 = strEdit.substr(pos+1);
}
else
{
     strPart1 = strEdit;
}
UnicodeString ustr1 = strPart1.c_str();
UnicodeString ustr2 = strPart2.c_str();

Open in new window


if not using UnicodeString but AnsiString or 'char *' then you would use std::string and not std::wstring.

#include <string>
...
std::string strEdit = strYourAnsiString.c_str();

std::string strPart1, strPart2;

size_t pos = strEdit.find('@');
if (pos != std::string::npos)
{
       strPart1 = strEdit.substr(0, pos);
       strPart2 = strEdit.substr(pos+1);
}
else
{
     strPart1 = strEdit;
}
AnsiString str1 = strPart1.c_str();
AnsiString str2 = strPart2.c_str();

Open in new window


if you have a TEdit variable you would get the text by member function TEdit::WindowText().

to find out whether the WindowText returns wide char pointer or char pointer, you may try to compile the statements:

// assume you have pointer to TEdit (pEdit)
wchar_t * pwsz = pEdit->WindowText();
char * psz = pEdit->WindowText();

Open in new window


only one of these statements will compile and you would know which type is required.

Sara
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rodric MacOliverResearcherAuthor Commented:
Sorry Sara, I'm not that good at C++ since I'm just starting to study it, so maybe I'm doing something completely stupid. :)

Here goes teh code:

	std::wstring strEdit = email->Text.c_str();

	std::wstring strPart1, strPart2;

	size_t pos = strEdit.find(L'@');
	if (pos != std::wstring::npos)
	{
		   strPart1 = strEdit.substr(0, pos);
		   user->Text = strPart1;                                    //ERROR POINTS HERE
		   strPart2 = strEdit.substr(pos+1);
		   domain->Text = strPart2;
	}
	else
	{
		 strPart1 = strEdit;
	}
	UnicodeString ustr1 = strPart1.c_str();
	UnicodeString ustr2 = strPart2.c_str();

Open in new window


The error says:
[bcc32 Error] Unit1.cpp(31): E2034 Cannot convert 'wstring' to 'UnicodeString'
  Full parser context
    Unit1.cpp(21): parsing: void _fastcall TForm1::Button1Click(TObject *)
0
Rodric MacOliverResearcherAuthor Commented:
I was mixing it up with the code and got it to work. Thanks a lot for the insights.
0
sarabandeCommented:
great.

i assume the statement

user->Text = strPart1;

Open in new window

was the wrong one which failed cause C++ Builder API  doesn't take std:.wstring bit a pointer to wchar_t instead.

so the statement

user->Text = strPart1.c_str();

Open in new window

should compile.

Sara
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.