简体   繁体   中英

Escape & in Java

I have this very basic SOAPElement.

The problem is that my password is test18T&T . I need a way to make the password with & like test18T&T

How can I do this? Is this already implemented behind the scenes?

Thanks

SOAPElement ticketUpdate = body.addChildElement("TicketUpdate", "con");

ticketUpdate.addChildElement("UserLogin", "con").addTextNode(user);
ticketUpdate.addChildElement("Password", "con").addTextNode(password);

I think what you are asking is something like this. Taking in mind that the & will always be there and we can use that as reference you can use the following.

input: test18T&T

output: test18T&T

SOAPElement ticketUpdate = body.addChildElement("TicketUpdate", "con");
ticketUpdate.addChildElement("UserLogin", "con").addTextNode(user);
String[] parts = password.split("&");
parts[0] = parts[0]+"&";
String newPassword = "";
for(String part : parts){
  newPassword = newPassword+part;
}
ticketUpdate.addChildElement("Password", "con").addTextNode(newPassword );

我能想到的最直接的方法是使用String API的全部替换功能

ticketUpdate.addChildElement("Password", "con").addTextNode(password.replaceAll("&", "&");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM