[英]how to remove special characters from string in a file using java
我有文本文件它包含以下信息。我的任務是從該文本文件中刪除特殊符號。我輸入文件conatins
This is sample CCNA program. it contains CCNP™
This is sample CCNA program. it contains CCNP™
。
我需要的輸出字符串:
This is sample CCNA program. it contains CCNP.
怎么做請建議我。
謝謝
這應該有效,“如果你想在字符串中只保留ASCII(0-127)字符”:
String str = "This is sample CCNA program. it contains CCNP™";
str = str.replaceAll("[^\\x00-\\x7f]+", "");
要刪除字符串中的所有特殊字符嗎? 如果是這樣的話:
String alphaOnly = input.replaceAll("[^a-zA-Z]+","");
String alphaAndDigits = input.replaceAll("[^a-zA-Z0-9]+","");
請參閱Sean Patrick Floyd對可能重復的問題的回答。
您可以從Unicode的角度來做:
String s = "This is sample CCNA program. it contains CCNP™. And it contains digits 123456789.";
String res = s.replaceAll("[^\\p{L}\\p{M}\\p{P}\\p{Nd}\\s]+", "");
System.out.println(res);
將打印出來:
這是CCNA計划的樣本。 它包含CCNP。 它包含數字123456789。
\\\\p{...}
是一個Unicode屬性
\\\\p{L}
匹配所有語言的所有字母
\\\\p{M}
用於與另一個角色(例如重音符號,變音符號,封閉框等)組合的角色。
\\\\p{P}
任何類型的標點字符。
\\\\p{Nd}
除了表意文字腳本之外的任何腳本中的數字0到9。
所以這個正則表達式將替換不是字母(也是組合字母),標點符號,數字或空格字符( \\\\s
)的每個字符。
^[\\u0000-\\u007F]*$
有了這個,你只允許ASCCI角色,但你需要告訴我們什么是特殊角色。
String yourString = "This is sample CCNA program. it contains CCNP™";
String result = yourString.replaceAll("[\\™]","");
System.out.println(yourString);
System.out.println(result);
您還可以嘗試以下方法:
Normalizer.decompose(str, false, 0).replaceAll("\\p{InSuperscriptsAndSubscripts}+", "");
但您需要找到適當的Unicode組或組( Unicode塊 )。
您必須真正定義實例中的特殊字符。
如果您不是RegEx的粉絲,可以考慮使用Character
類中的一些方法。 見下面的示例:
public class Test {
public static void main(String[] args) {
String test = "This is sample CCNA program. it contains CCNP™";
System.out.println("Character\tAlpha or Letter\tWhitespace");
for (char c : test.toCharArray()) {
System.out.println(
c + "\t\t"
+ Character.isLetterOrDigit(c) + "\t\t"
+ Character.isWhitespace(c));
}
}
}
除上述方法外,還可以使用其他方法。 查看Character
類API。
正則表達式的替代選項,以排除字符> 128。
String s = "This is sample CCNA program. it contains CCNP™";
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) > 128) {
s = s.substring(0, i)
+ s.substring(i + 1);
i++;
}
}
import java.util.Scanner;
public class replacespecialchar {
/**
* @param args
*/
public static void main(String[] args) {
String before="";
String after="";
Scanner in =new Scanner(System.in);
System.out.println("enter string with special char");
before=in.nextLine();
for (int i=0;i<before.length();i++)
{
if (before.charAt(i)>=65&&before.charAt(i)<=90 || before.charAt(i)>=97&&before.charAt(i)<=122)
{
after+=before.charAt(i);
}
}
System.out.println("String with special char "+before);
System.out.println("String without special char "+after);
}
}
關於刪除字符> 128的上述答案非常有用。 謝謝。
但是,它沒有涵蓋某些情況,例如連續的2個壞字符或字符串末尾的壞字符。 以下是我的修改,刪除除tab和新行之外的所有特殊字符。
// Remove all special characters except tab and linefeed
public static String cleanTextBoxData(String value) {
if (value != null) {
int beforeLen = value.length();
for (int i = 0; i < value.length(); i++) {
if ( ((value.charAt(i)<32) || (value.charAt(i)>126)) &&
((value.charAt(i)!=9) && (value.charAt(i)!=10)) ) {
if ((value.charAt(i)<32) || (value.charAt(i)>126)) {
if (i==value.length()-1) {
value = value.substring(0,i);
} else {
value = value.substring(0,i) + value.substring(i+1);
i--;
}
}
if (i == value.length()) {
break;
}
}
}
int dif = beforeLen - value.length();
if (dif > 0) {
logger.warn("Found and removed {} bad characters from text box.", dif);
}
}
return value;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.