有人可以用代码帮助我,以替换用点替换字符串中的替代逗号吗?
答案 0 :(得分:2)
如果我对您的理解正确,则可以通过以下两种方法进行操作:
通过使用public class BankOperationComputed {
private final BankOperation operation;
private final int delta;
BankOperationComputed (BankOperation operation, int delta){
this.operation = operation;
this.delta = delta;
}
public void startProcessing(){
System.out.println(operation.getOutput(delta));
}
}
模块here
BankOperation.computeBankOperation(5, 20) // returns BankOperationComputed
.startProcessing(); // uses the enum under the hoods
2或简单地:
re
欢迎堆栈溢出!如有疑问,请在以后更加清楚。我建议阅读this,以帮助您理解提出清晰问题的过程。
答案 1 :(得分:0)
some_string = 'one,two,three,four,five'
new_string = ''
replace = True # one.two,three.four,five
# replace = False # one,two.three,four.five
for i in some_string:
if i == ',' :
if replace :
new_string += '.'
else:
new_string += ','
replace = not replace # logic for alternating
else:
new_string += i
print(new_string) # one.two,three.four,five
#----------------------------------------------------
some_string = 'one,two,three,four,five,six'
new_string = ''.join([s+',' if i%2 == 0 else s+'.' for i,s in enumerate(some_string.split(','))])
print(new_string.strip(',').strip('.')) #one,two.three,four.five,six