去掉QString中的任意空白符

【免费】最新区块链钱包和私钥的助记词碰撞器,使用python开发 最新钱包私钥助记词碰撞器,搏一搏单车变摩托! 阅读详情

首先有这样的一个需求,去掉QString字符串中所有的空格,空白符\n和\t,那我们该怎么快速实现呢?
这个需求的一个应用场景就是,你可以把没有空格的字符串当做key,存放到map或者其他容器中。

正则表达式中\s匹配任何空白字符,包括空格、制表符、换页符等等, 等价于[ \f\n\r\t\v]

\f -> 匹配一个换页
\n -> 匹配一个换行符
\r -> 匹配一个回车符
\t -> 匹配一个制表符
\v -> 匹配一个垂直制表符
而“\s+”则表示匹配任意多个上面的字符。另因为反斜杠在C++里是转义字符,所以在C++里,我们要这么用“\s+”.

那么问题来了,“\s+”有啥使用场景呢?

QString &QString::remove(const QRegExp &rx)
Removes every occurrence of the regular expression rx in the string, and returns a reference to the string. For example:

所有用正则匹配到的,都会被QString.remove删掉,所以这里用"\s"还是"\s+"都是可以的,不信你可以试试

QString strSrc = "hello\t \t \n worl  d";
qDebug() << strSrc;
qDebug() << strSrc.remove(QRegExp("\\s"));
qDebug() << strSrc.remove(QRegExp("\\s+"));

运行结果:

"hello\t \t \n worl  d"
"helloworld"
"helloworld"

另外,Qt的正则表达式出了新款——QRegularExpression
也是一样的用法,唯一不同的是需要手动包含头文件

#include <QRegularExpression>
    QString strSrc = "hello\t \t \n worl  d";
    qDebug() << strSrc;
    qDebug() << strSrc.remove(QRegularExpression("\\s"));

运行结果:

"helloworld"

参考:
非常好用的正则表达式"\s+"

基于STM32设计的智能水产养殖系统(华为云IOT)(152) 当前设计的智能水产养殖系统主控芯片采用STM32F103RCT6,可以实现对水位、水温、水混浊度和PH值等参数的监测和控制,并且支持定时投喂功能。通过连接华为云物联网平台,将传感器数据上传到服务器,并通过Qt设计的Android手机APP来获取设备上传的数据,从而实现对养殖环境的远程监测和控制。 阅读详情

相关推荐

免费替代Cursor!TRAE中文版这些隐藏功能90%的人不知道

本文深度评测了免费AI编程工具TRAE中文版,揭示其超越代码补全的隐藏功能。重点介绍了其SOLO模式如何重塑从需求到部署的全流程开发,以及对微信小程序、Taro、Ant Design Pro等国产技术栈的深度适配能力,为中文开发者提供了高度本地化的高效解决方案。

gray5的博客 840

去掉Qstring换行符空格符

在Qt中,QString类提供了一些函数来处理字符串,包括去掉换行符。

future_ai的博客 5892

FreeFEM++下载安装和使用

最近要利用机器学习求解一些偏微分方程,对于一些复杂的偏微分方程,需要一些精度比较高的参考解,导师推荐了FreeFEM++软件。首次使用,特此记录一下,以备以后查阅。 1、FreeFEM++简介 FreeFEM是开源的有限元模拟系统,有法国利翁斯实验室、埃尔和玛丽居里大学共同开发,在世界范围内广泛使用。 2、FreeFEM++去何处下载? 下载地址:https://freefem.org/ 点击Download,然后会跳转到 https://github.com/FreeFem/FreeFem-source

夜晓岚的博客 1万+

QString去掉字符串中的空格

QString中去掉多余空格的方法有三种: 1. remove方法来实现 QString str = “aa ss ff jj”; str.remove(QRegExp(“\\s”)); 这种方式会去掉字符串中的所有空格。结果:”aassffjj” 2. simplified QString str = “ aa\t ss\n ff\r\n ”; str.simplified(); 这个方法会清楚字符串首尾的空格,且字符串中间的空格都转换为一个空格。结果:”aa ss ff” 3. tri

Pailugou的博客 1万+

lammps建模(电解液)opls-aa力场的基本流程

lammps建模(电解液)opls-aa力场的基本流程

syj0922的博客 2134

Blender入门教程-3D视图界面介绍

3D视图用于与3D场景交互,可用于各种目的,例如建模、动画、纹理绘制等。在关闭 启动画面 后,启动场景会显示在3D视口中(如果没有加载其他blend文件)。01 标题栏标题栏包含了基于当前 模式 的各种菜单和控制。

weixin_52345129的博客 2291

QT 删除QString中的空格/空白(QT系列12)

空白包括 \t', '\n', '\v', '\f', '\r', ' ' //删所有空白 str=str.remove(QRegExp("\\s")); //把中间空白改成一个空格 str = str.simplified(); //删头尾空白 str = str.trimmed();

qq_43690756的博客 3447

Simulink开发项1000例实战专栏--实例38:基于Simulink的周波变换器(Cycloconverter)

需求分析周波变换器(Cycloconverter),也称为频率变换器或直接变频器,是一种将输入的固定频率交流电直接转换为不同频率的交流电的电力电子设备。它广泛应用于工业领域,如大功率电机驱动、风力发电、船舶推进系统等场合。与传统的间接变频器(如交-直-交变频器)相比,周波变换器具有更高的效率和更简单的结构,适用于需要低频输出的应用场景。

xiaoheshang_123的博客 335

QString中去除空格的几种方法

使用正则表达式: QSring.remove(QRegExp("^ +\s*")); 实例: 2.去除字符串后面空格 使用正则表达式: QSring.remove(QRegExp("\s* +$")); 实例: 3.去除字符串中首尾空格 (1)QString QString::simplified() const 返回一个字符串,移除从一开始到结尾的空白,每个序列内部的空格替换为一个空格(头尾的都去掉了) 实例 ‘\t’, ‘\n’, ‘\v’, ‘\f’, ‘\r’, ’ ’ 都属于空白的

thanklife的专栏 8027

ClawManager实战:OpenClaw批量部署+精细化权限管控,运维效率拉满90%

ClawManager彻底解决了OpenClaw规模化部署的运维痛点,它不是简单的“批量部署工具”,而是一套完整的企业级管理方案——从模板配置、批量部署,到权限管控、监控审计、AI治理,覆盖OpenClaw全生命周期管理,让OpenClaw从“个人工具”升级为“企业级AI基础设施”。对于中小企业,Docker一键部署即可快速落地,无需复杂配置;对于大型企业,K8s部署支持高可用、横向扩展,结合Keycloak对接企业现有认证系统,可完美融入企业IT架构。

2601_94871597的博客 610

半导体设备系列(2) 半导体设备与工厂控制仿真器Demo编写

基于C/S架构用C++编写半导体设备仿真器与工厂控制仿真器

追赶时代的博客 610

Qt 判断QString是否为空

isEmpty(); QString().isEmpty(); // returns true QString("").isEmpty(); // returns true QString(“x”).isEmpty(); // returns false QString(“abc”).isEmpty(); // returns false isNull(); QString().isNull(); // return

Finull1的博客 1万+

YOLOv8【第十一章:视频追踪与流处理篇·第6节】BoT-SORT 改进:结合相机运动补偿(CMC)提升追踪稳定性!

🏆 本文收录于 《YOLOv8实战:从入门到深度优化》 专栏。该专栏系统复现并梳理全网各类 YOLOv8 改进与实战案例(当前已覆盖分类 / 检测 / 分割 / 追踪 / 关键点 / OBB 检测等方向),坚持持续更新 + 深度解析,质量分长期稳定在 97 分以上,可视为当前市面上 覆盖较全、更新较快、实战导向极强 的 YOLO 改进系列内容之一。

**My Coding Family** 719

QString 功能简介:各种去除空白符

这段时间回家,一直没有来得及写,今天才发现博客的编辑器有了新版。还是先来试试新版编辑器的功能吧! 今天要说的是QString。之所以把QString单独拿出来,是因为string是很常用的一个数据结构,甚至在很多语言中,比如JavaScript,都是把string作为一种同int等一样的基本数据结构来实现的。 每一个GUI程序都需要string,这些string可以用在界面上的提示语,也可以用

g457499940的专栏 1万+

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

QString 详解

本文转载,非原创,做了新增修改。 QString在Qt的各种数据转换中可谓举足轻重,熟悉QString的用法对于Qt编程真的是如虎添翼: QString 字符串有相当多的操作符和函数,此处只介绍一些比较常用的: (1) “+” 用于组合两个字符串,“+=” 用于将一个字符串追加到另一个字符串的末尾,例如: QString str1 = "Welcome"; str1 = str1 + "to you !"; //str1 = "Welcome to you!" QString str2 =...

yangyaokun0220的博客 2991

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

QT QString

一、QSting删除空白符 1、使用正则表达 str= str.remove( QRegExp("\\s") )//删除字符床中的所有空白符 2、使用simplified()方法 str = str.simplified()//删除字符串前后的所有空白符,中间的所有空白符使用单个空格替换 3、使用trimmed()方法 str = str.trimmed()//删除字符串前后的所有空白...

woquNOKIA的专栏 838

qt中的QString::number()的精度使用问题

1、 QString::number(average, 'f', 5),这里的'f'表示的是:‘f’方式结果是0.00,所以后面的5表示的就是输出的时候保留5位小数 2、这种的QString::number(average, ‘g’, 3); ,这里的'g'表示的是:’g’方式结果是1e-06,输出的时候保留3位小数 3、QString::trimmed()函数:移除字符串两端的空白符...

Littlehero_121的博客 1万+

Qstring

原地址:https://blog.youkuaiyun.com/qq_40194498/article/details/79629632QString在Qt的各种数据转换中可谓举足轻重,熟悉QString的用法对于Qt编程真的是如虎添翼:QString 字符串有相当多的操作符和函数,此处只介绍一些比较常用的:(1) “+” 用于组合两个字符串,“+=” 用于将一个字符串追加到另一个字符串的末尾,例如: QSt...

wang13342322203的博客 1542

Qt 中QString 字符串操作:连接、组合、替换、去掉空白字符

Qt中的字符串类QString类 保存了16位Unicode值,提供了丰富的操作、查询和转换等函数。 QString 字符串有如下几个操作符: (1) “+” 用于组合两个字符串,“+=” 用于将一个字符串追加到另一个字符串的末尾,例如: 1 QString str1 = "Welcome"; 2 str1 = str1 + "to you !"; //str1 = "Welco...

weixin_34366546的博客 3197

QString 的使用

Qt中的字符串类 QString类 保存了16位Unicode值,提供了丰富的操作、查询和转换等函数。 QString 字符串有如下几个操作符: (1) “+” 用于组合两个字符串,“+=” 用于将一个字符串追加到另一个字符串的末尾,例如: 1 QString str1 = “Welcome”; 2 str1 = str1 + “to you !”; //str1 = “Welcome to ...

it草稿纸 420

Qt删除 空格/空白符

  1、 QString str = " ad dfdsf ef okkd ikk"; str.remove(QRegExp("\\s")); qDebug()&lt;&lt;str.remove(QRegExp("\\s")); "addfdsfefokkdikk" 2、 还有去掉前后的空白符 Example: QString str = "  lots\t of\nwhites...

江南雨 1785

QString的常用方法

QString的常用方法总结。

柳旭辉的博客 3万+

QString字符串操作总结完整版

QString字符串操作总结完整版 //追加字符串 //QString提供一个二元+操作符来连接两个字符串和一个+=操作符来向一个字符串追加字符串。 QString str = "A"; QString str2 = "D"; str = str + "B"; str2 += "E"; //结果为, str为“AB”,str2为“DE” //组合字符串 //使用QString的sprintf()...

qq_35297310的博客 3441

QString用法详解(连接、替换、初始化与格式转换、拆分等等)

QString在Qt的各种数据转换中可谓举足轻重,熟悉QString的用法对于Qt编程真的是如虎添翼: QString 字符串有相当多的操作符和函数,此处只介绍一些比较常用的: (1) “+” 用于组合两个字符串,“+=” 用于将一个字符串追加到另一个字符串的末尾,例如: QString str1 = &quot;Welcome&quot;; str1 = str1 + &quot;to you !&quot;; //...

左霖风的博客 4万+

qstring去掉特定字符_跟我学Swift 5.2字符

介绍字符是 Swift 中的一种数据类型字符对应的类型关键字为Character后面要讲解的字符串可以理解为多个字符的集合字符的值只能包含一个值字符的值必须包含在""双引号里(单引号不行)字符是组成字符串的基本单位基本使用let char1: Character = "A"let char2: Character = "B"此时如果改成如下的代码就会报错,因为它们的值不止一个。// 报错...

weixin_39788382的博客 1203

QString常用的方法

QString常用的方法

xy_77777的博客 666
上一篇: Qt中利用QMetaEnum实现枚举(enum)类型值及字符串转换
下一篇: 谈一谈#define中#和##的作用
hp_cpp
博客等级 码龄11年 310粉丝 218原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值