QT__QString___操作

来源:互联网 发布:微信 js 播放器代码 编辑:程序博客网 时间:2024/06/04 01:29
更多内容可以直接看:http://doc.qt.digia.com/4.7-snapshot/qstring.html
#include <QtCore>#include <QDebug>//! [1]int main(int argc, char *argv[]){      QString str1="liming";    QString str2="You are";    str2+=str1+" !";  //“+”操作:    qDebug()<<str2;    str2.sprintf("%s","hello"); //sprintf操作    qDebug()<<str2;    str1="25";    str2="You are ";    str2.append(str1);//append()函数:相当于“+=”    qDebug()<<str2;    str2=QString("%l is a %2").arg("liming").arg("boy");//arg()函数:    qDebug()<<str2;    str2="Hello world";    qDebug()<<str2.startsWith("Hello",Qt::CaseSensitive);    qDebug()<<str2.contains("hello",Qt::CaseInsensitive);    str1="25";    bool ok;    int hex=str1.toInt(&ok,16); //以16进制转换    if(ok)        qDebug()<<hex;    double d=str1.toDouble(&ok);//以十进制转换。  ,如果字符串中有008,则转为8,去掉前面的00    if(ok)         qDebug()<<d;    return 0;}
======================一下是5月20日tested===================================
#include <QtGui/QApplication>#include <QtDebug>#include <QString>#include <QRegExp>  int main(int argc, char *argv[]){    QApplication a(argc, argv);    QString str = "string to char"; //QString转char *    QByteArray ba = str.toLatin1();    char *mm = ba.data();    qDebug()<<mm<<endl;     char buf[20]="char sToring";//char *转QString      QString nn = QString(QLatin1String(buf));    qDebug()<<nn<<endl;    nn.insert(2,"sd"); //插入    qDebug()<<nn<<endl;    char buf2[10]="To"; //查找字符串    int i=nn.indexOf(buf2);   ///index return the position of nn, start from 0;    qDebug()<<QString::number(i);    QString strr = "colour behaviour flavour neighbour"; //替换          strr.replace(QString("ou"), QString("X")); // strr == colXr behaviXr flavXr neighbXr"          qDebug()<<strr;    int retCmp=QString::compare(nn,mm);//静态比较两个字符串,返回大于0小于0等于0的整数    if(retCmp>0)        qDebug()<<QString("compare >0");    else if(retCmp==0)        qDebug()<<QString("compare ==0");    else        qDebug()<<QString("compare < 0");    QString str2 = "Peter Pan"; //是否包含     bool retbol=str2.contains("peter", Qt::CaseInsensitive);  //Returns true if this string contains an occurrence of the string str; otherwise returns false.     //  If cs is Qt::CaseSensitive (default), the search is case sensitive; otherwise the search is case insensitive.  // returns true     if (retbol)         qDebug()<<QString("test contains is here" );//Returns the number of times the regular expression rx matches in the string.//     This function counts overlapping matches, so in the example below, there are four instances of "ana" or "ama":     QString str3 = "banana and panama"; //查找出现的次数,频率     int nret=str3.count(QRegExp("a[nm]a"));    // returns 4     qDebug()<<QString::number(nret);//Returns a pointer to the data stored in the QString. The pointer can be used to access and modify the characters that compose the string. For convenience, the data is '\0'-terminated.//就是返回指向字符串的指针     QString str5 = "Hello world";     QChar *data = str5.data();     while (!data->isNull()) {         qDebug() << data->unicode();         ++data;     }     QString s = "Hello world";  //追加     s.resize(5);      qDebug()<<s;     // s == "Hello"     s.resize(8);     // s == "Hello???" (where ? stands for any character)     qDebug()<<s;   // If you want to append a certain number of identical characters to the string, use operator+=() as follows rather than resize():     QString t = "Hello";     t += QString(10, 'X');     // t == "HelloXXXXXXXXXX"     qDebug()<<t;     QString x = "Pineapple";     QString y = x.right(5);      // y == "apple"     qDebug()<<y;     size_t BufSize=100; //使用是sprintf输出给一个字符 man中有这样的话We do not recommend using QString::sprintf() in new Qt code     char buf3[BufSize];     ::snprintf(buf3, BufSize, "%lld", 123456789LL);     QString str6 = QString::fromAscii(buf3);     qDebug()<<str6;     /*QString & QString::fill(QChar c,int len=-1)   填充字符串为len个字符的值c并返回引用remove(int,int)起始位置和结束位置和 indexOf(字符串)查找字符串所在的位置并返回QString字符串类(三)2010-10-21 10:24QString的函数使用一,操作字符串:QString str2 = "Hello";这一行调用的QString类的构造函数是:QString::QString(const char *str)这个传递进来的const char *str指针以会被函数QString::fromAscii()转化为Unicode编码。默认情况下fromAscii()函数会反字符当作Latin-1编码处理。这就是为什么我们使用中文的时候会出现乱码。一种解决方法是使用 QTextCodec::setCodecForCString()函数改变QString:fromAscii()函数的默认行为。另一种方法是在编译的时候定义宏QT_CAST_FROM_ASCII来屏蔽上面的这个构造函数。如果程序的所有字符都必须经过 QObject::tr()函数处理,那么使用这个宏屏蔽掉这个构造函数会很方便。QString::append()函数和+=操作符有相同的作用,就是将一个字符串追加到另一个字符串的末尾。QString::sprintf()函数与C++中的sprintf()函数一样,可以将多个字符串及其他类型的数据以一定的格式组织顾一个字符串:QString::sprintf("%s was born in %d","John",1988);QString::arg()函数提供第三种字符串组合方式。这个函数是类型安全并且完全支持Unicode的。也是最常使用的:QString str = QString("%1 was born %2.").arg("John").arg("1982");QString::insert()函数在源字符串的指定位置插入另一个字符串。QString::prepend()函数在源字符串的开头位置插入另一个字符串。QString;:replace()函数用指定的字符串替换源字符串中的某些字符。QString::trimmed()函数移除字符串两端的空白字符。QString::simplifed()除了移除字符串两端的空白字符外,还会使用单个空格字符代替字符串中出现的空白字符。二,查询字符串数据:QString::startsWidth("xxx",Qt::CaseSensitive)判断一个字符串是否以某个字符串开头。第一个参数是指定的字符串。第二个参数指定是否大小写敏感(默认敏感)。QString::endsWidth()判断一个字符串否以某个字符是否以某个字符串结尾。QString::contains()判断一个指定的字符串是否出现过。三,比较字符串:operator<(const QString&)、operator<=(const QString&)、operator==(const QString&)、operator>=(const QString&)、operator>(cons QString&)。QString::localeAwareCompare(const QString&,const QString&)静态函数。比较两个字符串,小于返回负数;等于返回零;大于返回正数。该函数比较是基于本地字符集的,通常平台相关。QString::compare(const QString&,const QString&,Qt::CaseSensitivity)静态函数可以指定是否进行大小写敏感比较。大小写敏感比较是完全基于字符的Unicode编码值的。//QString 斜杠查找替换QString str ="colour\behaviour\flavour\"storedb.StoreProfile.ReceiptHeader.replace(QString("\\"), QString("\n"));//str ="colour\nbehaviour\flavour\n"//QString添加n个相同字符QString str;str.fill( 'g', 5 );       // string == "ggggg"2010-10-11 13:28QString 截取字串 字串处理QString str="wo,shi,tab";QStringList strlist = str.split(",");str1 = strlist.at(0).toLocal8Bit().data();str2 = strlist.at(1).toLocal8Bit().data();str3= strlist.at(2).toLocal8Bit().data();//str1="wo";//st2='shi";//str3 = "tab";QString str="wo,shi,tab";str=str.remove(QChar(','),Qt::CaseInsensitive);官方网站的说明:http://doc.qt.nokia.com/4.7-snapshot/qstring.html*/    return a.exec();}


从指定位置 读取n个字符 返回一个新的QString 用mid
 QString str=QString("123456789");   ///从指定位置截取一个字符串。
    QString str1=str.mid(str.indexOf("3")+1,2);
    qDebug()<<str1;

-----------------------------------------------------------------------------------------
同Java的String类类似,QString也重载的+和+=运算符。这两个运算符可以把两个字符串连接到一起,正像Java里面的操作一样。QString可以自动的对占用内存空间进行扩充,这种连接操作是恨迅速的。下面是这两个操作符的使用: 

QString str = "User: ";  str += userName + "\n"; QString的append()函数则提供了类似的操作,例如: 

str = "User: ";  str.append(userName);  str.append("\n"); C语言中有printf()函数作为格式化输出,QString则提供了一个sprintf()函数实现了相同的功能: 

str.sprintf("%s %.1f%%", "perfect competition", 100.0); 这句代码将输出:perfect competition 100.0%,同C语言的printf()一样。不过前面我们也见到了Qt提供的另一种格式化字符串输出的函数arg(): 

str = QString("%1 %2 (%3s-%4s)")        .arg("permissive").arg("society").arg(1950).arg(1970); 这段代码中,%1, %2, %3, %4作为占位符,将被后面的arg()函数中的内容依次替换,比如%1将被替换成permissive,%2将被替换成society,%3将被替换成 1950,%4将被替换曾1970,最后,这句代码输出为:permissive society (1950s-1970s). arg()函数比起sprintf()来是类型安全的,同时它也接受多种的数据类型作为参数,因此建议使用arg()函数而不是传统的 sprintf()。 
	
				
		
原创粉丝点击