我正在使用Twitter API,而且我有以下字符串让我烦恼Proyecto de ingeniera comercial, actual Profesora de matemáticas \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000Enseña Chile
我想将它存储在PostgreSql中,但不接受\u0000
,所以我想替换它。
我尝试使用string= string.replaceAll("\\u0000", "");
,但它不起作用。我刚刚得到以下
String json = TwitterObjectFactory.getRawJSON(user);
System.out.println(json);
json = json.replaceAll("\\u0000", "");
System.out.println(json);
输出(只有重要部分)
Proyecto de ingeniera comercial, actual Profesora de matemáticas \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000Enseña Chile
Proyecto de ingeniera comercial, actual Profesora de matemáticas \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000Enseña Chile
如果我把那个部分放在java中的一个字符串中替换有效,但如果我把它放在一个文本文件中,或者我直接为Twitter读它就不行了。所以我的问题是,如何替换\一个字符串的u0000?
顺便说一下,完整的字符串就是这个
{"utc_offset":null,"friends_count":83,"profile_image_url_https":"https://pbs.twimg.com/profile_images/2636139584/3a8455cd94045fa6980402add14796a9_normal.jpeg","listed_count":1,"profile_background_image_url":"http://abs.twimg.com/images/themes/theme1/bg.png","default_profile_image":false,"favourites_count":0,"description":"Proyecto de ingeniera comercial, actual Profesora de matemáticas \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000Enseña Chile","created_at":"Sat May 28 14:24:06 +0000 2011","is_translator":false,"profile_background_image_url_https":"https://abs.twimg.com/images/themes/theme1/bg.png","protected":false,"screen_name":"Fsquadritto","id_str":"306825274","profile_link_color":"0084B4","is_translation_enabled":false,"id":306825274,"geo_enabled":false,"profile_background_color":"C0DEED","lang":"es","profile_sidebar_border_color":"C0DEED","profile_location":null,"profile_text_color":"333333","verified":false,"profile_image_url":"http://pbs.twimg.com/profile_images/2636139584/3a8455cd94045fa6980402add14796a9_normal.jpeg","time_zone":null,"url":null,"contributors_enabled":false,"profile_background_tile":false,"entities":{"description":{"urls":[]}},"statuses_count":2,"follow_request_sent":false,"followers_count":36,"profile_use_background_image":true,"default_profile":true,"following":false,"name":"Fiorella Squadritto","location":"","profile_sidebar_fill_color":"DDEEF6","notifications":false,"status":{"in_reply_to_status_id_str":null,"in_reply_to_status_id":null,"possibly_sensitive":false,"coordinates":null,"created_at":"Fri Oct 12 17:40:35 +0000 2012","truncated":false,"in_reply_to_user_id_str":null,"source":"<a href=\"http://instagram.com\" rel=\"nofollow\">Instagram<\/a>","retweet_count":1,"retweeted":false,"geo":null,"in_reply_to_screen_name":null,"entities":{"urls":[{"display_url":"instagr.am/p/QsOQxTNfvQ/","indices":[49,69],"expanded_url":"http://instagr.am/p/QsOQxTNfvQ/","url":"http://t.co/GKziME7N"}],"hashtags":[{"indices":[24,34],"text":"eduinnova"}],"user_mentions":[{"indices":[35,47],"screen_name":"ensenachile","id_str":"57099132","name":"Enseña Chile","id":57099132}],"symbols":[]},"id_str":"256811615171792896","in_reply_to_user_id":null,"favorite_count":1,"id":256811615171792896,"text":"Amando las matemáticas! #eduinnova @ensenachile http://t.co/GKziME7N","place":null,"contributors":null,"lang":"es","favorited":false}}
答案 0 :(得分:17)
string = string.replace("\u0000", ""); // removes NUL chars
string = string.replace("\\u0000", ""); // removes backslash+u0000
具有u-escaping的角色在java源代码级别完成。例如&#34; class&#34;是:
public \u0063lass C {
此外,您不需要正则表达式。
答案 1 :(得分:3)
replaceAll
的第一个参数是正则表达式,Java正则表达式引擎理解\uNNNN
转义为
json.replaceAll("\\u0000", "")
将搜索正则表达式 \u0000
,它匹配实际字符串的Unicode NUL字符(U + 0000), not 实例的实例{ {1}}。如果要匹配字符串\u0000
,则需要使用正则表达式\u0000
,这反过来表示Java字符串文字\\u0000
"\\\\u0000"
或者更简单地说,使用json.replaceAll("\\\\u0000", "")
(其第一个参数是文字字符串而不是正则表达式)而不是replace
replaceAll
答案 2 :(得分:0)
在某些情况下,输入文本包含多个反斜杠,后跟 \u0000。处理所有情况
<块引用> String test = "ABC\\u0000DEF\\\\u0000123";
System.out.println(test.replaceAll("[\\\\]+u0000",""));