字符串 #

一、字符串概述 #

MATLAB中有两种文本类型:

类型 创建方式 描述
字符串(string) 双引号 " " R2016b引入,推荐使用
字符数组(char) 单引号 ' ' 传统类型,兼容性好
matlab
% 字符串(推荐)
str1 = "Hello World";
class(str1)    % 'string'

% 字符数组
str2 = 'Hello World';
class(str2)    % 'char'

二、创建字符串 #

2.1 基本创建 #

matlab
% 双引号创建
str1 = "Hello";
str2 = "世界";
str3 = "123";

% 空字符串
empty_str = "";

% 字符串数组
str_arr = ["Hello", "World", "MATLAB"];

2.2 特殊字符 #

matlab
% 转义字符
str1 = "Line1\nLine2";      % 换行
str2 = "Tab\there";         % 制表符
str3 = "Quote: \"";         % 双引号
str4 = "Backslash: \\";     % 反斜杠

% 显示
disp(str1);
% Line1
% Line2

2.3 多行字符串 #

matlab
% 使用换行符
str1 = "Line 1" + newline + "Line 2";

% 使用字符串数组
str2 = ["Line 1"; "Line 2"; "Line 3"];

% 使用三引号(R2021b+)
str3 = """Line 1
Line 2
Line 3""";

三、字符串操作 #

3.1 拼接 #

matlab
% 使用加号
str1 = "Hello";
str2 = "World";
result = str1 + " " + str2;    % "Hello World"

% 使用join
strs = ["Hello", "World", "MATLAB"];
result = join(strs);           % "Hello World MATLAB"
result = join(strs, "-");      % "Hello-World-MATLAB"

% 使用strcat
result = strcat(str1, str2);   % "HelloWorld"(注意无空格)

% 使用append
result = append(str1, " ", str2);  % "Hello World"

3.2 分割 #

matlab
% split函数
str = "Hello World MATLAB";
parts = split(str);        % ["Hello"; "World"; "MATLAB"]

% 指定分隔符
str = "a,b,c,d";
parts = split(str, ",");   % ["a"; "b"; "c"; "d"]

% splitlines
str = "Line1\nLine2\nLine3";
lines = splitlines(str);   % ["Line1"; "Line2"; "Line3"]

3.3 提取子串 #

matlab
str = "Hello World";

% 使用索引
sub1 = str(1:5);           % "Hello"
sub2 = str(7:end);         % "World"
sub3 = str(end-4:end);     % "World"

% extractBetween
str = "<html>Content</html>";
content = extractBetween(str, "<html>", "</html>");  % "Content"

% extractAfter/extractBefore
str = "file_data.txt";
name = extractBefore(str, ".");     % "file_data"
ext = extractAfter(str, ".");       % "txt"

3.4 替换 #

matlab
str = "Hello World";

% replace
new_str = replace(str, "World", "MATLAB");  % "Hello MATLAB"

% replaceBetween
str = "Start [remove this] End";
new_str = replaceBetween(str, "[", "]", "content");
% "Start [content] End"

% erase
str = "Hello World World";
new_str = erase(str, "World");  % "Hello  "

% eraseBetween
str = "Start [remove] End";
new_str = eraseBetween(str, "[", "]");  % "Start [] End"

3.5 大小写转换 #

matlab
str = "Hello World";

upper(str)     % "HELLO WORLD"
lower(str)     % "hello world"

% 首字母大写
str = "hello world";
capitalize(str)   % "Hello World"(R2021b+)

3.6 去除空白 #

matlab
str = "  Hello World  ";

strip(str)           % "Hello World"(两端)
strtrim(str)         % "Hello World"(两端)
lstrip(str)          % "Hello World  "(左端)
rstrip(str)          % "  Hello World"(右端)

% 去除特定字符
str = "***Hello***";
strip(str, '*')      % "Hello"

四、字符串查找 #

4.1 查找位置 #

matlab
str = "Hello World World";

% strfind
pos = strfind(str, "World");    % [7, 13]

% contains
has = contains(str, "World");   % true

% startsWith/endsWith
str = "test_file.txt";
startsWith(str, "test")    % true
endsWith(str, ".txt")      % true

4.2 匹配模式 #

matlab
% 使用模式匹配
str = "file123.txt";

% contains模式
contains(str, digitsPattern)    % true(包含数字)

% extract
str = "abc123def456";
nums = extract(str, digitsPattern);  % ["123"; "456"]

% 正则表达式
str = "Hello 123 World 456";
nums = regexp(str, '\d+', 'match');  % {'123', '456'}

五、字符串比较 #

5.1 基本比较 #

matlab
str1 = "Hello";
str2 = "World";
str3 = "Hello";

% 相等比较
str1 == str2      % false
str1 == str3      % true

% strcmp
strcmp(str1, str2)    % 0(false)
strcmp(str1, str3)    % 1(true)

% 忽略大小写
strcmpi("Hello", "HELLO")    % 1(true)

5.2 排序比较 #

matlab
str1 = "apple";
str2 = "banana";

% 比较大小
str1 < str2       % true(按字典序)

% strncmp(比较前n个字符)
strncmp("Hello", "Help", 3)    % 1(前3个字符相同)

六、字符串与数值转换 #

6.1 字符串转数值 #

matlab
% str2double
str = "3.14159";
num = str2double(str);    % 3.14159

% str2num
str = "[1 2 3; 4 5 6]";
arr = str2num(str);       % [1 2 3; 4 5 6]

% sscanf
str = "123 456 789";
nums = sscanf(str, '%d'); % [123; 456; 789]

% 处理转换失败
str = "abc";
num = str2double(str);    % NaN
isnan(num)                % true

6.2 数值转字符串 #

matlab
% string函数
num = 123;
str = string(num);        % "123"

% num2str
str = num2str(3.14159);        % "3.14159"
str = num2str(3.14159, 2);     % "3.1"
str = num2str(3.14159, '%.2f'); % "3.14"

% int2str
str = int2str(42);        % "42"

% mat2str
arr = [1 2; 3 4];
str = mat2str(arr);       % "[1 2;3 4]"

七、字符串格式化 #

7.1 sprintf格式化 #

matlab
% 基本格式化
name = "张三";
age = 25;
str = sprintf("%s今年%d岁", name, age);
% "张三今年25岁"

% 格式说明符
% %s - 字符串
% %d - 整数
% %f - 浮点数
% %e - 科学计数法
% %g - 自动选择%f或%e
% %c - 单个字符
% %% - 百分号

% 精度控制
pi_str = sprintf("%.4f", pi);    % "3.1416"
sci_str = sprintf("%.2e", 1234); % "1.23e+03"

% 宽度和对齐
str = sprintf("%10s", "Hello");  % "     Hello"(右对齐)
str = sprintf("%-10s", "Hello"); % "Hello     "(左对齐)
str = sprintf("%05d", 42);       % "00042"(前导零)

7.2 fprintf输出 #

matlab
% 输出到命令窗口
fprintf("Hello %s\n", "World");

% 输出到文件
file = fopen('output.txt', 'w');
fprintf(file, "数据: %d\n", 42);
fclose(file);

7.3 compose函数 #

matlab
% R2016b+推荐使用
str = compose("值: %.2f", 3.14159);  % "值: 3.14"

% 批量格式化
values = [1.1, 2.2, 3.3];
strs = compose("Item %.1f", values);
% ["Item 1.1"; "Item 2.2"; "Item 3.3"]

八、字符串数组 #

8.1 创建字符串数组 #

matlab
% 方括号创建
strs = ["Hello", "World", "MATLAB"];

% 字符串函数
strs = string({"apple", "banana", "cherry"});

% 从数值创建
nums = [1, 2, 3];
strs = string(nums);    % ["1", "2", "3"]

% 重复
strs = repmat("Hello", 3, 1);  % 3x1字符串数组

8.2 操作字符串数组 #

matlab
strs = ["apple", "banana", "cherry"];

% 长度
lengths = strlength(strs);    % [5, 6, 6]

% 连接
result = join(strs, ", ");    % "apple, banana, cherry"

% 查找
idx = find(contains(strs, "a"));    % [1, 2, 3]

% 筛选
filtered = strs(contains(strs, "app"));  % "apple"

% 对每个元素操作
upper_strs = upper(strs);     % ["APPLE"; "BANANA"; "CHERRY"]

九、正则表达式 #

9.1 基本使用 #

matlab
str = "Hello 123 World 456";

% 匹配
matches = regexp(str, '\d+', 'match');
% {'123', '456'}

% 查找位置
pos = regexp(str, '\d+');    % [7, 15]

% 替换
new_str = regexprep(str, '\d+', 'NUM');
% "Hello NUM World NUM"

% 分割
parts = regexp(str, '\s+', 'split');
% {'Hello', '123', 'World', '456'}

9.2 常用模式 #

matlab
% 数字
\d+        % 一个或多个数字
\d{3}      % 恰好3个数字
\d{2,4}    % 2到4个数字

% 字母
[a-z]+     % 小写字母
[A-Z]+     % 大写字母
[a-zA-Z]+  % 所有字母

% 特殊字符
\w+        % 单词字符(字母、数字、下划线)
\s+        % 空白字符
\W+        % 非单词字符

% 边界
^          % 开头
$          % 结尾
\b         % 单词边界

% 示例:提取邮箱
str = "Contact: test@example.com";
email = regexp(str, '\w+@\w+\.\w+', 'match');
% {'test@example.com'}

十、实用示例 #

10.1 文件名处理 #

matlab
filename = "data_2024_03_27.csv";

% 提取日期
date_part = extractBetween(filename, "data_", ".csv");
% "2024_03_27"

% 分割日期
parts = split(date_part, "_");
year = parts(1);    % "2024"
month = parts(2);   % "03"
day = parts(3);     % "27"

% 构建新文件名
new_name = "result_" + year + month + day + ".txt";
% "result_20240327.txt"

10.2 数据解析 #

matlab
% 解析CSV格式数据
line = "张三,25,北京,工程师";

% 方法1:split
fields = split(line, ",");
name = fields(1);
age = str2double(fields(2));
city = fields(3);
job = fields(4);

% 方法2:textscan
data = textscan(line, '%s%d%s%s', 'Delimiter', ',');

10.3 文本处理 #

matlab
% 统计单词频率
text = "Hello World Hello MATLAB World World";
words = split(text);
unique_words = unique(words);
for i = 1:length(unique_words)
    count = sum(words == unique_words(i));
    fprintf("%s: %d\n", unique_words(i), count);
end
% Hello: 2
% MATLAB: 1
% World: 3

十一、总结 #

本章学习了:

  1. 字符串创建:双引号创建string类型
  2. 字符串操作:拼接、分割、提取、替换
  3. 字符串查找:strfind、contains、正则表达式
  4. 字符串比较:strcmp、strcmpi
  5. 类型转换:字符串与数值互转
  6. 格式化:sprintf、fprintf、compose
  7. 字符串数组:创建和操作
  8. 正则表达式:模式匹配和替换

下一章将学习字符数组的详细内容。

最后更新:2026-03-27