擅长:python、mysql、java
<p><strong>一种脓性溶液:</strong></p>
<pre><code>'\n'.join(e.replace('\n', ' ') for e in s.split('\n\n'))
</code></pre>
<p>首先,拆分出现<code>'\n\n'</code>的字符串,然后用<code>'\n'</code>替换列表中每个元素中的<code>' '</code>,最后,用<code>'\n'</code>连接所有内容。你知道吗</p>
<p><strong>另一种解决方案</strong></p>
<p>我将使用一个临时辅助字符串,它允许我临时替换双新行,然后用空格替换单新行,然后用新行替换临时辅助字符串:</p>
<p>假设您的字符串是<code>s</code>:</p>
<pre><code>s = "2\nOur\nstrategy drives \nsustainably higher profits and margins\n\nStrengthening our hubs is a critical foundation to maximize profitability\n\nDriving revenue improvements from all areas of business\n\nImproving efficiency and productivity \n\nGreater accountability and transparency\n"
temp = '-\t-'
s = s.replace('\n\n', temp).replace('\n', ' ').replace(temp, '\n')
</code></pre>