目录
搜索和替换文本
可以搜索字符数组和字符串数组中的文本,并将子字符串替换为新文本。R2016b 中引入了字符串数组以及可搜索和替换文本的新函数。使用 contains
等函数搜索子字符串。同样地,使用 replace
函数替换字符串中的文本,或使用 extractBetween
等函数提取文本。可以将上述任意函数用于字符向量或字符串数组。为实现兼容性,也可以将 strfind
和 strrep
等函数同时用于字符向量和字符串数组。
搜索文本
使用 contains
、startsWith
和 endsWith
函数标识字符串数组、字符向量或字符向量元胞数组中的文本。创建一个字符串。从 R2017a 开始,可以使用双引号创建字符串。
str = "Rosemary Jones"
str =
"Rosemary Jones"
确定 str
是否包含子字符串 mary
。如果 contains
函数在该字符串中的任意位置找到此子字符串,则会返回逻辑值 1
。
TF = contains(str,"mary")
TF = logical
1
也可以使用 strfind
函数查找匹配的文本。strfind
会返回每个匹配项开头字符的索引。在本例中,strfind
返回的是 5
,因为 mary
中的 m
是 str
的第五个字符。
idx = strfind(str,"mary")
idx = 5
使用 strfind
查找多个匹配项。存在多个匹配项时,strfind
会以数组形式返回这些索引。
idx = strfind(str,"s")
idx = 1×2
3 14
创建一个包含许多名字的字符串数组。确定哪些名字包含子字符串 Ann
。contains
函数会返回一个逻辑数组,只要 str
带有一个包含 Ann
的元素,该数组即包含一个 1
。要创建一个仅包含匹配项的新字符串数组,请使用 TF
对 str
进行索引。
str = ["Rosemary Ann Jones","Peter Michael Smith","Ann Marie Young"]
str = 1x3 string array
"Rosemary Ann Jones" "Peter Michael Smith" "Ann Marie Young"
TF = contains(str,"Ann")
TF = 1x3 logical array
1 0 1
matches = str(TF)
matches = 1x2 string array
"Rosemary Ann Jones" "Ann Marie Young"
查找以 Ann
开头的字符串。
TF = startsWith(str,"Ann");
matches = str(TF)
matches =
"Ann Marie Young"
同样地,endsWith
函数可查找以指定的文本片段结尾的字符串。
也可以使用 contains
、startsWith
和 endsWith
函数来确定字符向量是否包含文本。
chr = 'John Paul Jones'
chr =
'John Paul Jones'
TF = contains(chr,'Paul')
TF = logical
1
TF = endsWith(chr,'Paul')
TF = logical
0
使用 contains
函数查找字符串数组的行中的文本。census1905
包含数行 1905 年的模拟普查数据。每一行都包含一个名字、出生年份以及当年用该名字起名的次数。
census1905 = ["Ann Mary","1905","230";
"John","1905","5400";
"Mary","1905","4600";
"Maryjane","1905","304";
"Paul","1905","1206"];
查找名字等于 Mary
的行。
TF = (census1905(:,1) == "Mary");
census1905(TF,:)
ans = 1x3 string array
"Mary" "1905" "4600"
使用 contains
函数查找名字属于 Mary
的变化形式的行。
TF = contains(census1905(:,1),"Mary");
census1905(TF,:)
ans = 3x3 string array
"Ann Mary" "1905" "230"
"Mary" "1905" "4600"
"Maryjane" "1905" "304"
替换文本
可以使用 replace
函数替换字符串数组、字符向量或字符向量元胞数组中的文本。创建一个字符串。将子字符串 mary
替换为 anne
。
str = "Rosemary Jones"
str =
"Rosemary Jones"
newStr = replace(str,"mary","anne")
newStr =
"Roseanne Jones"
也可以使用 strrep
函数来替换文本。但是,建议使用 replace
函数。
newStr = strrep(str,"Jones","Day")
newStr =
"Rosemary Day"
创建一个包含许多名字的字符串数组。
str = ["Rosemary Ann Jones","Peter Michael Smith","Ann Marie Young"]
str = 1x3 string array
"Rosemary Ann Jones" "Peter Michael Smith" "Ann Marie Young"
指定要替换的多个名字。
oldText = ["Ann","Michael"];
newText = ["Beth","John"];
newStr = replace(str,oldText,newText)
newStr = 1x3 string array
"Rosemary Beth Jones" "Peter John Smith" "Beth Marie Young"
替换字符向量中的文本。可以将 replace
和 replaceBetween
用于字符向量和字符串。
chr = 'Mercury, Gemini, Apollo'
chr =
'Mercury, Gemini, Apollo'
replace(chr,'Gemini','Mars')
ans =
'Mercury, Mars, Apollo'
替换文件名字符串数组中的文本。将文件名追加到网站地址。文件名中包含空格,但空格不能是 Web 地址的一部分。将空格字符 " "
替换为 %20
(这是 Web 地址的标准)。
str = ["Financial Report.docx";
"Quarterly 2015 Details.docx";
"Slides.pptx"]
str = 3x1 string array
"Financial Report.docx"
"Quarterly 2015 Details.docx"
"Slides.pptx"
newStr = replace(str," ","%20")
newStr = 3x1 string array
"Financial%20Report.docx"
"Quarterly%202015%20Details.docx"
"Slides.pptx"
将文件名追加到网站地址。
filenames = "http://example.com/Documents/" + newStr
filenames = 3x1 string array
"http://example.com/Documents/Financial%20Report.docx"
"http://example.com/Documents/Quarterly%202015%20Details.docx"
"http://example.com/Documents/Slides.pptx"
提取文本
使用 extractAfter
、extractBefore
和 extractBetween
函数从字符串数组或字符向量中提取子字符串。使用上述函数可提取位于指定的文本片段之前、之后或中间的不同子字符串。创建一个包含文件名的字符串数组。使用 extractAfter
函数提取位于 C:\Temp\
之后的名称部分。
str = ["C:\Temp\MyReport.docx";
"C:\Temp\Data\Sample1.csv";
"C:\Temp\Slides.pptx"]
str = 3x1 string array
"C:\Temp\MyReport.docx"
"C:\Temp\Data\Sample1.csv"
"C:\Temp\Slides.pptx"
filenames = extractAfter(str,"C:\Temp\")
filenames = 3x1 string array
"MyReport.docx"
"Data\Sample1.csv"
"Slides.pptx"
从在 XML 标记内对名称进行编码的字符串数组中提取客户名称。
str = ["<CustomerName>Elizabeth Day</CustomerName>";
"<CustomerName>George Adams</CustomerName>";
"<CustomerName>Sarah Young</CustomerName>"]
str = 3x1 string array
"<CustomerName>Elizabeth Day</CustomerName>"
"<CustomerName>George Adams</CustomerName>"
"<CustomerName>Sarah Young</CustomerName>"
names = extractBetween(str,"<CustomerName>","</CustomerName>")
names = 3x1 string array
"Elizabeth Day"
"George Adams"
"Sarah Young"