Solved

Substring with start and end

Posted on 2014-10-21
8
274 Views
Last Modified: 2014-10-24
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!
0
Comment
Question by:R Wolf
8 Comments
 
LVL 32

Expert Comment

by:sarabande
Comment Utility
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
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
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
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
Comment Utility
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
 

Author Comment

by:R Wolf
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 32

Accepted Solution

by:
sarabande earned 500 total points
Comment Utility
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
 

Author Comment

by:R Wolf
Comment Utility
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
 

Author Closing Comment

by:R Wolf
Comment Utility
I was mixing it up with the code and got it to work. Thanks a lot for the insights.
0
 
LVL 32

Expert Comment

by:sarabande
Comment Utility
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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to useā€¦
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilationā€¦
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now