matlab:字符串数组和string的区别
2017-04-17 09:08
1071 查看
前言
在用sprintf()格式化输出时,发现formatSpec可以是单引号创建的字符串数组,也可以是string(‘str’)创建的string。所以产生了疑惑,这两者有什么区别,各适用在什么场合官方文档
Character arrays and string arrays provide storage for text data in MATLAB®. A character array is a sequence of characters, just as a numeric array is a sequence of numbers. A typical use is to store short pieces of text as character vectors, such as c = 'Hello World'. A string array is a container for pieces of text. String arrays provide a set of functions for working with text as data. Starting in R2017a, you can create strings using double quotes, such as str = "Greetings friend". To convert data to string arrays, use the string function.
代码
str=sprintf('pi = %.5f',pi); %输出类型为char str1=sprintf(string('pi = %.5f'),pi); %输出类型为string
输出结果:
%测试字符串数组 >> class(str) ans = char >> str(1) ans = p >> size(str) ans = 1 12 %测试string >> class(str1) ans = string >> size(str1) ans = 1 1 >> str1(1) ans = string "pi = 3.14159"
总结
由文档中可看出:1. 字符串数组就相当于数据类型为char的矩阵(向量)。用单引号创建。可以索引。
2. string相当于一个类。用string()函数创建实例对象。所以有一些函数可以对string进行操作。size为1*1。
相关文章推荐
- 字符数组和字符串,char 和string定义的字符串的区别与遍历
- 『C++ Primer学习笔记』Chapter 3——string对象、vector对象以及数组的配套类型,C风格字符串问题和处理区别
- char字符串数组与string之间的转换,以及sizeof,strlen,length的区别
- 字符数组和字符串,char 和string定义的字符串的区别与遍历
- String中空串与null的区别#抽取方法快捷键#字节数组转化成字符串#字符数组转化成字符串
- 【读书笔记】std::string的基本操作 与字符串数组的区别
- 字符串string 字符数组与指向字符串的指针pchar的区别与联系
- JAVA——(1)基础补充之运算符、字符串、数组、内存分配(String,StringBuffer,StringBuilder的区别
- 字符数组和字符串,char 和string定义的字符串的区别与遍历
- <MFC>CString和<C++>标准库string的区别,及判断字符串是否为空
- C++字符串数组赋值与单个字符赋值区别
- 字符串切分,String和StringUtils的区别
- c#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换
- 字符串string、内存流Memory、Stream 流 及比特数组byte[] 互转方法汇总
- 字符串数组String[] arr转List<String>
- String的split()方法可以将字符串按照特定的分隔符拆分成字符串数组
- JavaScript中String(a)和new String(a)两种生成字符串的方法的使用区别
- 字符串数组(String []) 去掉重复值的方法
- 字符串数组(String []) 去掉重复值的方法
- 字符串指针与一维指针数组的区别