【问题标题】:MATLAB: How to divide up a string according to a recurring numberMATLAB:如何根据重复数字划分字符串
【发布时间】:2021-01-18 15:09:41
【问题描述】:

美好的一天。

下面是我要分文的一个例子:

str =      ["1 This is sentence one of verse one, "+ ...
            "2 This is sentence one of verse two. "+ ...
            "3 This is sentence two of verse three; "+ ...
            "1 This is sentence three of verse one? "+ ...
            "2 This is sentence four of verse two, "+ ...
            "3 this is sentence four but verse three!"];

注意数字是如何重新开始的,这表明新的章节开始了。如何制作一个 nx1 字符串,其中 n=chapters.结果应该是这样的:

感谢您的帮助。 谢谢!

【问题讨论】:

  • 不应该有3行(诗句)吗?图为2行
  • 好眼光。是的。有什么想法吗?

标签: string matlab conditional-statements division


【解决方案1】:

你只需要一点点字符串操作:

>> c=convertStringsToChars(str);
>> splitstring = split(str,c(1));
>> splitstring = splitstring(strlength(splitstring) > 0);
>> formattedstring = string(1:length(splitstring)).' + " " + c(1) + splitstring

formattedstring = 

  2×1 string array

    "1 1 This is sentence one of verse one, 2 This is sentence one of verse two. 3 This is sentence two of verse three; "
    "2 1 This is sentence three of verse one? 2 This is sentence four of verse two, 3 this is sentence four but verse three!"

【讨论】:

    猜你喜欢
    • 2020-06-11
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 2011-08-04
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 2020-09-25
    相关资源
    最近更新 更多