strin()函数

版权声明:小简原创 https://blog.csdn.net/qq_43469554/article/details/87611851

String(number, character)
String 函数的语法有下面的命名参数:
number 必要参数;Long。返回的字符串长度。如果 number 包含 Null,将返回 Null。
character 必要参数;Variant。为指定字符的字符码或字符串表达式,其第一个字符将用于建立返回的字符串。如果 character 包含 Null,就会返回 Null。
注意number是返回的字符串长度,而不是character的重复次数。
实例:
z=string(3,“w”)
z的返回值是:www
z=string(3,“aw”)
z的返回值是:awa
z=string(3,“www”)
z的返回值是:www

说明:
如果指定 character 的数值大于 255,String 会按下面的公式将其转为有效的字符码:
character Mod 256

应用实例:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		string space = string(n - i, ' ');
		string ch = string(2 * i - 1, 'A' + i - 1);
		cout << space + ch << endl;
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_43469554/article/details/87611851