Qt学习——字符串类QString

来源:互联网 发布:采集数据的方法有哪些 编辑:程序博客网 时间:2024/05/17 09:19
 
  1. 操作字符串:
  • “+”操作: 
    view plaincopy to clipboardprint?
    1. QString str1="25";   
    2. QString str2="You are ";  
    3.  str2+=str1+"!";  

  • append()函数:相当于“+=”
    view plaincopy to clipboardprint?
    1. QString str1="25";  
    2. QString str2="You are ";   
    3. str2.append(str1);  

  • sprintf()函数:
    view plaincopy to clipboardprint?
    1. QString str;  
    2. str.sprintf("%s","hello");  

  • arg()函数:
    view plaincopy to clipboardprint?
    1. QString str;  
    2. str=QString("%1 is a %2").arg("liming").arg("boy");  


  • trimmed()函数去除字符串两端空白字符
    view plaincopy to clipboardprint?
    1. QString str="    Zhao Liming   ";  
    2. str.trimmed();  

 2.查询字符串:
  • startsWith()函数:
    view plaincopy to clipboardprint?
    1. QString str="Hello World";  
    2. str.startsWith("Welcome",Qt::CaseSensitive);//返回true  

  • contains()函数:
    view plaincopy to clipboardprint?
    1. str.contains("Welcome",Qt::CaseSensitive);//返回true  

  • <,<=,==,>=等操作符
 3.字符串转换:
  • toInt()函数转换字符串到int类型: 
    view plaincopy to clipboardprint?
    1. QString str1="25"bool ok;  
    2. int hex=str1.toInt(&ok,16);//ok返回是否转换成功,转换为16进制,默认为10进制  

  • toDouble(),toAscii(),toUtf8()等。
http://blog.csdn.net/a649518776/article/details/6630964