宣传方案怎么写java中字符串中⼤于⼩于号的转义替换项⽬中需要将前端传过来的特殊字符做转义处理,然后在保存到数据库中,⽐如说“<”,">",但是替换⼀直失败
先看错误的做法:(不能达到替换效果)
public class StringTest {
public static void main(String[] arg){
String s = "0<R<=150";
if (s.contains("<=")) {
System.out.println("1s=" + s);
}
}
}
输出:1s=0<R<=150国富论全称
正确的做法:(达到替换效果)
public class StringTest {
public static void main(String[] arg){
劳动的诗句String s = "0<R<=150";
if (s.contains("<=")) {
s = s.replace("<=", "≤");//必须要重新赋值给⼀个字符串
System.out.println("1s=" + s);
数字插画}
}
}
输出:1s=0<R≤150
原因见api:
replace
public String replace(char oldChar,
char newChar)
返回⼀个新的字符串,它是通过⽤ newChar 替换此字符串中出现的所有 oldChar 得到的。
如果 oldChar 在此 String 对象表⽰的字符序列中没有出现,则返回对此 String 对象的引⽤。否则,创建⼀个新的 String 对象,它所表⽰的字符序列除了所有的 oldChar 都被替换为 newChar 之外,与此 String 对象表⽰的字符序列相同。
⽰例:
"mesquite in your cellar".replace('e', 'o')
returns "mosquito in your collar"
"the war of baronets".replace('r', 'y')
returns "the way of bayonets"
"sparring with a purple porpoi".replace('p', 't')
returns "starring with a turtle tortoi"
"JonL".replace('q', 'x') returns "JonL" (no change)
参数:
oldChar - 原字符。
newChar - 新字符。
返回:
⼀个从此字符串派⽣的字符串,它将此字符串中的所有 oldChar 替代为 newChar。
附 HTML特殊字符编码⼤全:
´´©©>>µµ®®
&&°°¡¡ »»
¦¦÷÷¿¿¬¬§§
·•½½««¶¶¨¨
¸¸¼¼<<±±××
¢¢¾¾¯¯""™™
€€££¥¥
…„……··››ªª
ˆˆ““——’’ºº
††‹‹––‚‚””团拜
如何清洗地暖
‡‡‘‘‰‰˜˜
≈≈⁄⁄←←∂∂♠♠∩∩≥≥≤≤″″∑∑
♣♣↔↔◊◊′′↑↑
↓↓♥♥−−∏∏
♦♦∞∞≠≠√√
骑的英文≡≡∫∫‾‾→→
ααηημμππθθββγγννψψυυχχιιωωρρξξ
δδκκοοσσζζ
εελλφφττ
ΑΑΗΗΜΜΠΠΘΘΒΒΓΓΝΝΨΨΥΥΧΧΙΙΩΩΡΡΞΞ
ΔΔΚΚΟΟΣΣΖΖ非主流图
ΕΕΛΛΦΦΤΤς&sigmaf;要替换好多特殊字符了!