[Qt-interest] How do cut the Qstring

Arnold Krille arnold at arnoldarts.de
Thu Jun 18 15:04:56 CEST 2009


On Thursday 18 June 2009 07:42:00 邵度 wrote:
> I think the simplest way is:
> *mail*.split("@")[0].split(":")[1]
> *mail* is the QString instance you want to extract.
> 2009/6/18 Yuvaraj R <yuvaraj at ongobiz.com>
> >  I am having the QString like
> > sip:username at roameio.com <sip%3Ausername at roameio.com>
> > from the string i need username only... How can i get username only....

Even simplier:

string.section("@", 0).section(":",1);

Or you could use QString;;mid():

string.mid( string.indexOf(":"), string.indexOf("@") - string.indexOf(":") );

(Could be there is a off-by-one error in that last one...)

I would actually _not_ convert to c-strings. QString has good multibyte-char 
support, you don't want your app to fail just because my username is "ärnöld" 
:-)

Another thing that comes to mind: Are these strings always "just" 
protocol:username at domain.org or can the also be 
protocol://username:password@domain.org ? Because then you need a different 
split. Actually why don't you use QUrl and just extract the username by 
QUrl::userName() ?

Have fun,

Arnold
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090618/e22947b4/attachment.bin 


More information about the Qt-interest-old mailing list