`

String.trim()方法去掉的不仅仅是空格

阅读更多


今天在做马士兵老师的聊天雏形系统项目时,因为想模仿QQ的聊天界面,于是把输入框从TextField类型换成了TextArea,然后从KeyAdapter类继承,重写keyReleased(KeyEvent e) 方法,虽然实现了功能,但是老多出一个换行符,想用String.replaceAll()方法替换掉换行符却达不到想要的效果(一直以为trim()方法只去掉空格);后来查了API文档才发现 trim() 去除的不仅仅是空格.


以下是trim()的API说明:

public String trim()
Returns a copy of the string, with leading and trailing whitespace omitted.

If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than '\u0020' (the space character), then a reference to this String object is returned.

Otherwise, if there is no character with a code greater than '\u0020' in the string, then a new String object representing an empty string is created and returned.

Otherwise, let k be the index of the first character in the string whose code is greater than '\u0020', and let m be the index of the last character in the string whose code is greater than '\u0020'. A new String object is created, representing the substring of this string that begins with the character at index k and ends with the character at index m-that is, the result of this.substring(km+1).

This method may be used to trim whitespace (as defined above) from the beginning and end of a string.

Returns:
A copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.
红字部分说得很清楚:若字符串中没有代码大于'\u0020'的字符,则创建并返回一个代表空字符串的新的String对象.
以下是我在keyReleased()方法中的写法:
private class EnterMonitor extends KeyAdapter {
		public void keyReleased(KeyEvent ke) {
			int k = ke.getKeyCode();
			if(k == KeyEvent.VK_ENTER) {
				try {
					dos.writeUTF(tf.getText().trim());
					//调用trim()方法去除按下ENTER时出现的换行符
					dos.flush();
					tf.setText("");
				}catch(IOException ioe) {
					ioe.printStackTrace();
				}
			}
		}
	}
  

 

分享到:
评论

相关推荐

    Js里面给String添加trim()方法,实现去掉字符串两边空格

    String.trim()Js里面给String添加trim()方法,实现去掉字符串两边空格String.trim()Js里面给String添加trim()方法,实现去掉字符串两边空格String.trim()Js里面给String添加trim()方法,实现去掉字符串两边...

    js String.prototype.trim字符去前后空格的扩展

    最近学习js的时候发现的这个函数,这样很方便地去除前后空格,用正则实现,简单方便。下面软件开发网小编就为大家分享一下几种实现方式。 String.Prototype.trim() trim()返回一个字符串两端空白字符被删除的新字符...

    jquery $.trim()去除字符串空格的实现方法【附图例】

    jquery $.trim()去除字符串空格的实现方法【附图例】 语法 jQuery.trim()函数用于去除字符串两端的空白字符。 作用 该函数可以去除字符串开始和末尾两端的空白字符(直到遇到第一个非空白字符串为止)。它会清除包括换...

    C#实现去除Strings中空格的方法

    一般来说,你或许知道你能使用String.Trim方法去除字符串的头和尾的空格,不幸运的是. 这个Trim方法不能去除字符串中间的C#空格。 示例代码如下: 代码如下:string text = ” My test\nstring\r\n is\t quite long...

    js 自定义trim去除字符串左右杂质

    JAVA中String 有trim()方法去除字符串左右的空格,js中自定义trim方法,去除字符串左右的杂质,可以去除逗号、句号、空格等等特殊字符。

    JavaScript trim 去除字符串空格的三种方法(附代码详解)

    方法一: 正则替换 推荐个人认为最好的方法.采用的是正则表达式,这是最核心的原理. 下面是代码原文 代码如下: [removed] <!– //出处:网上搜集 //For more visit //www.jb51.net // Trim() , Ltrim() , RTrim() ...

    C# 实现Trim方法去除字符串前后的所有空格

    在C#语言程序开发过程中,很多时候需要对字符串对象的前后空格进行去除,此时就需要使用到Trim()方法来实现这个功能,Trim()方法可以快速去除字符串前端和后端的所有空格。 例如有个字符: string str=” Abc ...

    Delphi Trim删除字符串首尾空格的实例.rar

    Delphi 删除字符串首尾空格的实例,过滤字符串,替换字符串,删除指定部位的字符串,具体到本例中,是删除字符串开头和结尾处的空格,本例子其实简单,使用内置的Trim函数即可实现,可当作是Delphi Trim函数的用法...

    javascript去除字符串左右两端的空格

    去除字符串左右两端的空格,在vbscript里面可以轻松地使用 trim、ltrim 或 rtrim,但在js中却没有这3个内置方法,需要手工编写。下面的实现方法是用到了正则表达式,效率不错,并把这三个方法加入String对象的内置...

    c++ String去除头尾空格的方法

    本文实例讲述了c++ String去除头尾空格的方法,分享给大家供大家参考。具体实现方法如下: 实现该功能可使用string的find_first_not_of,和find_last_not_of方法,具体实现带如下: 代码如下:#include #include ...

    js验证大全.txt

    *1、Trim=去除字符串前后空格 使用方法:String.trim() *2、ctrim=去除字符串中间空格 使用方法:String.ctrim() *3、onClickSelect=点中text框的时候,选中其中的文字 使用方法:在input位置加上 onClick/onFocus=...

    Javascript 修改String 对象 增加去除空格功能(示例代码)

    代码如下://#region 去除空格String.prototype.Trim = function () { return this.replace(/(^\s*)|(\s*$)/g, “”);} String.prototype.LTrim = function () { return this.replace(/(^\s*)/g, “”);} String....

    proposal-string-trim-characters:建议为 .trim()、.trimStart() 和 .trimEnd() 添加参数以允许从字符串中去除指定的字符

    我们经常通过trim 、 trimStart和trimEnd从字符串的开头或结尾(或两者)删除一些前导/尾随空格。 我们只能删除和 。 如果您想删除一些未由空格定义的其他字符串。 没有语义和方便的方法来做到这一点。 语义和便利 ...

    js去空格技巧分别去字符串前后、左右空格

    分别去字符串前后,左边,右边空格 代码如下: String.prototype.trim = function(){ return this.replace(/^\s+|\s+$/g,””)} String.prototype.ltrim = function(){ return this.replace(/^\s+/g,””)} String....

    js去除空格的12种实用方法

    String.prototype.trim = function() { return this.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); } 看起来不怎么样, 动用了两次正则替换,实际速度非常惊人,主要得益于浏览器的内部优化。一个著名的例子字符串...

    JavaScript自定义方法实现trim()、Ltrim()、Rtrim()的功能

    去除字符串两端的空格,是字符串处理非常常用的方法,非常遗憾的是JavaScript没有这三个方法,只有我们自定义了: 第1步,给String添加成员 代码如下: String.prototype.Trim = function(){ return Trim(this);...

    c#字符串去掉空格的二种方法(去掉两端空格)

    使用字符串的方法: trim();去掉字符串两端空格 split();切割 string.join();连接 代码如下:class Program { static void Main(string[] ... //去掉两端空格 str= str.Trim(); //以空格切割 string [] strArr

    JavaScript去掉空格的方法集合

    实现1 代码如下: String.prototype.trim = function () { return this .replace(/^\s\s*/, ” ).replace(/\s\s*$/, ” ); } 看起来不怎么样,动用了两次正则替换,实际速度非常惊人,主要得益于浏览器的内部优化。一...

    javascript去掉前后空格的实例

    function String.prototype.Trim() { return this.replace(/(^/s*)|(/s*$)/g, “”); } // 去掉左右空格 function String.prototype.Ltrim() { return this.replace(/(^/s*)/g, “”); } // 去掉左空格 function ...

    vue中如何去掉空格的方法实现

    首先可以使用v-model.trim这个v-model修饰符去解决它,但是当用户输入\u200B时,这个方法就不奏效了,这时我们可以去一下v-model.trim这个修饰符的源码 function genDefaultModel ( el: ASTElement, value: ...

Global site tag (gtag.js) - Google Analytics