我有一个字符串,其中每次有数字时都没有空格。我想创建它并添加一个逗号。
例如,以下内容:
2013 Presidential2008 Presidential2016 Parliamentary - Majoritarian Runoff2016 Parliamentary - Majoritarian Rerun
将转换为:
2013 Presidential,2008 Presidential,2016 Parliamentary - Majoritarian Runoff2016 Parliamentary - Majoritarian Rerun
直到现在我有了:
for char in s:
... if char.isalpha():
???
我也尝试过使用Javascript:
function isnumber(c) {
return (i >= '0' && i <= '9');
}
for (var x = 0; x < text.length; x++)
{
var c = text.charAt(x);
if isnumber(i){
// add a "," before and move to the next char which is a letter
text[:x] + ',' + text[x:]
}
}
但是它返回:Uncaught SyntaxError: Unexpected identifier