我们如何用明确读取的值替换下面突出显示的值?
String msg ="BGM+EFD:20:109:GBP+aa:bb:cc+5+dd'CST++KindOfDeclaration+0'";
String bgm=msg.split("\\'") [0];
String tempMessage=bgm;
String messageFunction="56";
System.out.println(bgm);
if(StringUtils.isNotEmpty(messageFunction)){
bgm=bgm.replace(bgm.split("\\+")[3],messageFunction);
}
else{
bgm=bgm.replace(bgm.split("\\+")[3],"9");
}
msg=msg.replace(tempMessage, bgm);
即我需要从服务器读取值。它正确读取它,但它也取代了之前的值109.
例如,如果它正在读取的值是56,它将109替换为1056,这是我不需要的。我已经给出了如下代码
BGM+EFD:20:109:GBP+aa:bb:cc+56+dd'CST++KindOfDeclaration+0'";
必需的输出是
BGM+EFD:20:1056:GBP+aa:bb:cc+56+dd'CST++KindOfDeclaration+0'";
但我得到输出为
@IBOutlet weak var bottomLayoutConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShowNotification(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHideNotification(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// remove the observers so the code won't be called all the time
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWillShowNotification(notification: NSNotification) {
updateBottomLayoutConstraintWithNotification(notification:notification)
}
func keyboardWillHideNotification(notification: NSNotification) {
updateBottomLayoutConstraintWithNotification(notification:notification)
}
func updateBottomLayoutConstraintWithNotification(notification: NSNotification) {
let userInfo = notification.userInfo!
// get data from the userInfo
let animationDuration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
let keyboardEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let convertedKeyboardEndFrame = view.convert(keyboardEndFrame, from: view.window)
let rawAnimationCurve = (notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).uint32Value << 16
let animationCurve = UIViewAnimationOptions(rawValue: UInt(rawAnimationCurve))
bottomLayoutConstraint.constant = (view.bounds).maxY - (convertedKeyboardEndFrame).minY
// animate the changes
UIView.animate(withDuration: animationDuration, delay: 0.0, options: [.beginFromCurrentState, animationCurve], animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
答案 0 :(得分:0)
由于您的实际代码正在运行(我得到的输出不是您发布的错误的代码),这里是另一个使用正则表达式的解决方案:
这是一个简单的正则表达式,可以进行几乎相同的转换
msg = msg.replaceAll(
"(.*\\+)(\\d+)(\\+.*'.*)",
"$1"
+ (StringUtils.isNotEmpty(messageFunction) ? messageFunction : "9" )
+ "$3"
)
这个正则表达式"(.*\\+)(\\d+)(\\+.*'.*)"
只是让我在特定组中的'
之前得到“+ +”之间的最后一个值。我想这可以改进。但结果是正确的。
group 1 : (.*\\+)
group 2 : (\\d+) the value you seek
group 3 : (\\+.*'.*)
我不是正则表达式的高手,所以它可能不是100%安全但是仍然可以改进。