如何将字符串复制到C ++中的固定长度字符串 [英] How to copy string into fixed length string in C++

查看:912
本文介绍了如何将字符串复制到C ++中的固定长度字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,我想复制到一个固定长度的字符串。例如,我有一个 string s =这是一个字符串,长度为16个字符。

I have a string which I want to copy into a fixed length string. For example I have a string s = "this is a string" that is 16 characters long.

将它复制到长度为4个字符的固定长度字符串 s2 。因此 s2 将包含this

I want to copy this into a fixed length string s2 that is 4 characters long. So s2 will contain "this".

I也想将它复制到固定长度的字符串 s3 ,这是20个字符长。

I also want to copy it into a fixed length string s3 that is 20 characters long. The end of the string will have extra spaces since the original string is only 16 characters long.

推荐答案

s.resize(expected_size,' '); 

这篇关于如何将字符串复制到C ++中的固定长度字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆