日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

记录一次socket编程:String的trim函数

發布時間:2025/3/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 记录一次socket编程:String的trim函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

源碼:

/*** Returns a string whose value is this string, with any leading and trailing* whitespace removed.* <p>* If this {@code String} object represents an empty character* sequence, or the first and last characters of character sequence* represented by this {@code String} object both have codes* greater than {@code '\u005Cu0020'} (the space character), then a* reference to this {@code String} object is returned.* <p>* Otherwise, if there is no character with a code greater than* {@code '\u005Cu0020'} in the string, then a* {@code String} object representing an empty string is* returned.* <p>* Otherwise, let <i>k</i> be the index of the first character in the* string whose code is greater than {@code '\u005Cu0020'}, and let* <i>m</i> be the index of the last character in the string whose code* is greater than {@code '\u005Cu0020'}. A {@code String}* object is returned, representing the substring of this string that* begins with the character at index <i>k</i> and ends with the* character at index <i>m</i>-that is, the result of* {@code this.substring(k, m + 1)}.* <p>* This method may be used to trim whitespace (as defined above) from* the beginning and end of a string.** @return A string whose value is this string, with any leading and trailing white* space removed, or this string if it has no leading or* trailing white space.*/public String trim() {int len = value.length;int st = 0;char[] val = value; /* avoid getfield opcode */while ((st < len) && (val[st] <= ' ')) {st++;}while ((st < len) && (val[len - 1] <= ' ')) {len--;}return ((st > 0) || (len < value.length)) ? substring(st, len) : this;}

trim函數功能是去除首位的空格,但是需要注意的是,如果單單自身使用了trim函數,并沒有賦值給實例,則,該自身沒有去除空格。

例:

String s = "aaaa "; s.trim(); System.out.println(s+"*"); s = s.trim(); System.out.println(s+"*");

在socket編程的時候,我接收服務端傳過來的數據,用了直接trim函數,并沒有賦值給自身,因為它已經轉成去除空餓的字符串了,導致后端接收的數據一直有多余的空格。

?

總結

以上是生活随笔為你收集整理的记录一次socket编程:String的trim函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。