日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2025/3/15 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 记录一次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函数的全部內容,希望文章能夠幫你解決所遇到的問題。

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