Replacing char sequence with char from string in java
I want to replace all llpp with "<" char and all llqq with ">" from following string in a java code,
llpphtmlllqq
llppheadllqqllpptitlellqqCompany Name-Companyllpp/titlellqqllpp/headllqq
llppbodyllqqSome text herellpp/bodyllqq
llpp/htmlllqq
I have above string and i want it to be
<html>
<head&g开发者_StackOverflow社区t;<title>Company Name-Company</title></head>
<body>Some text here</body>
</html>
please help me
String targetString = sourceString.replace("llpp", "<").replace("llqq", ">");
The replace
method replaces each instance of the first parameter with the second parameter. Since String
s are immutable, you have to capture the result of the method by assigning it to a variable.
Note that the above code will effect the replacements you want, though it won't format the code the way you've shown. Hopefully that's not something you need, since that would be significantly more complicated.
String newString = oldString.replace("llpp", "<").replace("llqq", ">");
replace(CharSequence, CharSequence)
is available from Java 5. before that you had to use replaceAll
which takes regular expression as first parameter.
Use String#replaceAll
.
If in java have a look at String's replace function http://download.oracle.com/javase/1,5.0/docs/api/java/lang/String.html#replace%28java.lang.CharSequence,%20java.lang.CharSequence%29
精彩评论