【Qt5学习笔记】 2.1_字符串类
  TEZNKK3IfmPf 12天前 23 0
字符串拼接的五种方法
/******************************操作字符串******************************/
/***first***/
QString str1="welcome ";//str1="welcome"
str1=str1+"to you! ";//str1="welcome to you!"
qDebug("str1=%s\n",qPrintable(str1));
/***second***/
QString str2="Hellow! ";//str2="Hellow! "
str1.append(str2);//str1="welcome to you! Hellow! "
str1.append("you! ");//str1="welcome to you! Hellow! you! "
qDebug("str1=%s\n", qPrintable(str1));
/***third***/
QString str3;
str3.sprintf("%s", " welcome ");//str3= " welcome "
str3.sprintf("%s", " to you! ");//str3=" welcome to you! "
str3.sprintf("%s %s", "Welcome", "to you! ");//str3="Welcome to you! "
qDebug("str3=%s\n", qPrintable(str3));
/***fourth***/
QString str4;
str4=QString("%1 was born in %2.").arg("John").arg(1982);//str="John was born in 1982"
qDebug("str4=%s\n", qPrintable(str4));
字符串规范
/***字符串规范***/
QString str5="          Welcome \t to \n you       ";
qDebug("str5=%s\n",qPrintable(str5));
qDebug("str5.trimmed()=%s\n", qPrintable(str5.trimmed()));//移除字符串两端的空格
qDebug("str5.simplified()=%s\n", qPrintable(str5.simplified()));//移除字符串两端的空格,并使用" "代替字符串中出现的空白字符或者换行

查询字符串数据

/*****************************查询字符串数据******************************/
QString str="Welcome to you! ";
/***查询开头***/
bool b=str.startsWith("welcome", Qt::CaseSensitive);//大小写敏感(输出false)
b=str.startsWith("welcome", Qt::CaseInsensitive);//大小写不敏感(输出true)
b=str.startsWith("you", Qt::CaseSensitive);//大小写敏感(输出false)
/***查询结尾***/
b=str.endsWith("you! ",Qt::CaseSensitive);//输出true
/***查询此字符串是否出现过***/
b=str.contains("Welcome", Qt::CaseSensitive);//输出true

字符串转换

/******************************字符串转换**********************************/
QString str6="125";
bool ok;
int hex=str6.toInt(&ok, 16);//把125当作16进制转化为int型的10进制293,转化成功则ok为true
qDebug("%d\n", hex);//输出293
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 12天前 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年05月17日   30   0   0 字符串php
TEZNKK3IfmPf