A. java怎樣去掉string中的空格,回車符
java中String有個trim()能夠去掉一個字元串的前後空格。
但是trim()只能去掉字元串中前後的半形空格,而無法去掉全形空格。
去掉全形空格需要在trim()方法的基礎上加上一些判斷。
String textContent ="abctest";
textContent = textContent.trim();
while (textContent.startsWith("")) {//這里判斷是不是全形空格
textContent = textContent.substring(1, textContent.length()).trim();
}
while (textContent.endsWith("")) {
textContent = textContent.substring(0, textContent.length() - 1).trim();
}
B. Java中怎麼處理多餘的回車符
JAVA中去掉空格
1. String.trim()
trim()是去掉首尾空格
2.str.replace(" ", ""); 去掉所有空格,包括首尾、中間
復制代碼 代碼如下:String str = " hell o ";
String str2 = str.replaceAll(" ", "");
System.out.println(str2);
3.或者replaceAll(" +",""); 去掉所有空格
4.str = .replaceAll("\\s*", "");
可以替換大部分空白字元, 不限於空格
\s 可以匹配空格、製表符、換頁符等空白字元的其中任意一個 您可能感興趣的文章:java去除字元串中的空格、回車、換行符、製表符的小例子
C. Java 去掉文本框的回車符,讓游標回到起點
對TextArea設置按鍵的監聽事件(KeyListener),在重寫keyReleased方法的時候,將監聽到的enter鍵的時候,作游標處理,比如將內容清空,游標就回到了起點。
D. java怎麼把字元串後面的回車去掉
回車符號一般都是「\n」,把這個找出來,然後替換為""就可以了
E. Java如何去除字元串中的空格、回車、換行符、製表符
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
publicclassStringUtils{
/**
*正則
*/
(Stringstr){
Stringdest="";
if(str!=null){
Patternp=Pattern.compile("\s*| | | ");
Matcherm=p.matcher(str);
dest=m.replaceAll("");
}
returndest;
}
publicstaticvoidmain(String[]args){
System.out.println(StringUtils.replaceBlank("justdoit!"));
}
/*-----------------------------------
笨方法:Strings="你要去除的字元串";
1.去除空格:s=s.replace('\s','');
2.去除回車:s=s.replace(' ','');
這樣也可以把空格和回車去掉,其他也可以照這樣做。
註: 回車(u000a)
水平製表符(u0009)
s空格(u0008)
換行(u000d)*/
}
F. java中如何去掉輸入的文本裡面的回車鍵啊存數據時不能有回車。
你把回車符去掉就是了。
回車符在windows一般是"\n",你看你文本能不能讀出\n來。能的話,可以直接使用String的String str = "ab\nc";str.replaceAll("\n", "")方法來解決。如果讀不出\n來,就用下面的方法,下面的方法肯定行。
String str = "ab\nc";
String value = "";
for(int i=0;i<str.length();i++) {
if(str.codePointAt(i)!=10&&str.codePointAt(i)!=13) {
value += str.charAt(i);
}
}
System.out.println(value);
10跟13一個表示是\n,一個是表示\r,具體哪個是哪個我不記得了
G. java中怎麼替換掉回車換行符
java中替換回車換行符,如果是字元串的話,你可以使用string.replace函數來進行替換.
H. java 去掉回車符
在程序同一目錄下建a.txt文本文件測試。
import java.io.*;
class A
{
public static void main(String args [])
{
String context=null;
StringBuffer contextbuffer = new StringBuffer("");
String line = null;
try
{
FileReader fr = new FileReader("./a.txt");
BufferedReader br = new BufferedReader(fr);
while ((line = br.readLine()) != null)
{
contextbuffer.append(line);
}
br.close();
context = contextbuffer.toString();
FileWriter fw = new FileWriter("./a.txt",false);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(context);
bw.flush();
bw.close();
}
catch (Exception e) { }
}
}
I. java怎樣去掉空格符和回車符
(Stringstr){
if(str!=null&&!"".equals(str)){
Patternp=Pattern.compile("\s*| | | ");
Matcherm=p.matcher(str);
StringstrNoBlank=m.replaceAll("");
returnstrNoBlank;
}else{
returnstr;
}
}
用這個方法