【问题标题】:Replace a non-specific substring in a string替换字符串中的非特定子字符串
【发布时间】:2020-10-04 19:55:16
【问题描述】:

好吧,我正在尝试发现如何在这样的字符串中替换: String user ="123124.Ken:000"; 子字符串 .Ken:000 与例如 .Diana:999。我认为使用 indexOf()'.' 字符有一些东西,但我不知道如何从 indexOf() 替换到 user 字符串的末尾。

【问题讨论】:

  • 你应该把字符串解析成一个域类User,然后改变你需要改变的属性,最后再把你的域对象序列化成字符串。
  • replacereplaceAll 方法呢?

标签: java string replace substring indexof


【解决方案1】:
id = user.split(".")[0] # this will have the value of 123124
new user = id + ".Diana:999" 

你可以这样写一个简单的函数。

def changeUser (oldUser, user):
    return oldUser.split('.')[0] + user

可以使用拆分功能

【讨论】:

  • 这对我有用,假设 .Ken:000 可能会有所不同(因为 userKen 的名称),我认为 replacereplaceAll 不适合。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-10
  • 1970-01-01
  • 1970-01-01
  • 2012-08-09
  • 1970-01-01
相关资源
最近更新 更多