免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1212 | 回复: 0
打印 上一主题 下一主题

String ※ StringBuffered类 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-08-02 22:56 |只看该作者 |倒序浏览
[color="#000000"]本文主要讲解字符串操作,及其常用处理方法。主要摘录自Think In Java,附带本人的粗略翻译,主要是为了方便阅读学习。
The String and StringBuffer classes [color="#000000"]Here is an overview of the methods available for both [color="#000000"]String[color="#000000"] and [color="#000000"]StringBuffer[color="#000000"] so you can get a feel for the way they interact. These tables don’t contain every single method, but rather the ones that are important to this discussion. Methods that are overloaded are summarized in a single row. [color="#000000"]First, the [color="#000000"]String[color="#000000"] class: [color="#000000"]Method [color="#000000"]Arguments, Overloading [color="#000000"]Use [color="#000000"]Constructor [color="#000000"]Overloaded: Default, [color="#000000"]String[color="#000000"], [color="#000000"]StringBuffer, char [color="#000000"]arrays, [color="#000000"]byte[color="#000000"] arrays. [color="#000000"]Creating [color="#000000"]String[color="#000000"] objects. [color="#000000"]length( ) [color="#000000"]Number of characters in [color="#000000"]String[color="#000000"]. [color="#000000"]charAt() [color="#000000"]int Index [color="#000000"]The [color="#000000"]char[color="#000000"] at a location in the [color="#000000"]String[color="#000000"]. [color="#000000"]getChars( )[color="#000000"], [color="#000000"]getBytes( ) [color="#000000"]The beginning and end from which to copy, the array to copy into, an index into the destination array. [color="#000000"]Copy [color="#000000"]char[color="#000000"]s or [color="#000000"]bytes[color="#000000"] into an external array. [color="#000000"]toCharArray( ) [color="#000000"]Produces a [color="#000000"]char[][color="#000000"] containing the characters in the [color="#000000"]String[color="#000000"]. [color="#000000"]equals( )[color="#000000"], [color="#000000"]equals-IgnoreCase( ) [color="#000000"]A [color="#000000"]String[color="#000000"] to compare with. [color="#000000"]An equality check on the contents of the two [color="#000000"]String[color="#000000"]s. [color="#000000"]compareTo( ) [color="#000000"]A [color="#000000"]String[color="#000000"] to compare with. [color="#000000"]Result is negative, zero, or positive depending on the lexicographical ordering of the [color="#000000"]String[color="#000000"] and the argument. Uppercase and lowercase are not equal! [color="#000000"]regionMatches( ) [color="#000000"]Offset into this [color="#000000"]String[color="#000000"], the other [color="#000000"]String[color="#000000"] and its offset and length to compare. Overload adds “ignore case.” [color="#000000"]Boolean result indicates whether the region matches. [color="#000000"]startsWith( ) [color="#000000"]String[color="#000000"] that it might start with. Overload adds offset into argument. [color="#000000"]Boolean result indicates whether the [color="#000000"]String[color="#000000"] starts with the argument. [color="#000000"]endsWith( ) [color="#000000"]String[color="#000000"] that might be a suffix of this [color="#000000"]String[color="#000000"]. [color="#000000"]Boolean result indicates whether the argument is a suffix. [color="#000000"]indexOf( )[color="#000000"], [color="#000000"]lastIndexOf( ) [color="#000000"]Overloaded: [color="#000000"]char[color="#000000"], [color="#000000"]char [color="#000000"]and starting index, [color="#000000"]String[color="#000000"], [color="#000000"]String,[color="#000000"] and starting index [color="#000000"]Returns -1 if the argument is not found within this [color="#000000"]String[color="#000000"], otherwise returns the index where the argument starts. [color="#000000"]lastIndexOf( )[color="#000000"] searches backward from end. [color="#000000"]substring( ) [color="#000000"]Overloaded: Starting index, starting index, and ending index. [color="#000000"]Returns a new [color="#000000"]String[color="#000000"] object containing the specified character set. [color="#000000"]concat( ) [color="#000000"]The [color="#000000"]String[color="#000000"] to concatenate [color="#000000"]Returns a new [color="#000000"]String[color="#000000"] object containing the original [color="#000000"]String[color="#000000"]’s characters followed by the characters in the argument. [color="#000000"]replace( ) [color="#000000"]The old character to search for, the new character to replace it with. [color="#000000"]Returns a new [color="#000000"]String[color="#000000"] object with the replacements made. Uses the old [color="#000000"]String[color="#000000"] if no match is found. [color="#000000"]toLowerCase( ) toUpperCase( ) [color="#000000"]Returns a new [color="#000000"]String[color="#000000"] object with the case of all letters changed. Uses the old [color="#000000"]String[color="#000000"] if no changes need to be made. [color="#000000"]trim( ) [color="#000000"]Returns a new [color="#000000"]String[color="#000000"] object with the white space removed from each end. Uses the old [color="#000000"]String[color="#000000"] if no changes need to be made. [color="#000000"]valueOf( ) [color="#000000"]Overloaded: [color="#000000"]Object[color="#000000"], [color="#000000"]char[][color="#000000"], [color="#000000"]char[][color="#000000"] and offset and count, [color="#000000"]boolean[color="#000000"], [color="#000000"]char[color="#000000"], [color="#000000"]int[color="#000000"], [color="#000000"]long[color="#000000"], [color="#000000"]float[color="#000000"], [color="#000000"]double[color="#000000"]. [color="#000000"]Returns a [color="#000000"]String[color="#000000"] containing a character representation of the argument. [color="#000000"]intern( ) [color="#000000"]Produces one and only one [color="#000000"]String[color="#000000"] handle for each unique character sequence. [color="#000000"]You can see that every [color="#000000"]String[color="#000000"] method carefully returns a new [color="#000000"]String[color="#000000"] object when it’s necessary to change the contents. Also notice that if the contents don’t need changing the method will just return a handle to the original [color="#000000"]String[color="#000000"]. This saves storage and overhead. [color="#000000"]Here’s the [color="#000000"]StringBuffer[color="#000000"] class: [color="#000000"]Method [color="#000000"]Arguments, overloading [color="#000000"]Use [color="#000000"]Constructor [color="#000000"]Overloaded: default, length of buffer to create, [color="#000000"]String[color="#000000"] to create from. [color="#000000"]Create a new [color="#000000"]StringBuffer[color="#000000"] object. [color="#000000"]toString( ) [color="#000000"]Creates a [color="#000000"]String[color="#000000"] from this [color="#000000"]StringBuffer[color="#000000"]. [color="#000000"]length( ) [color="#000000"]Number of characters in the [color="#000000"]StringBuffer[color="#000000"]. [color="#000000"]capacity( ) [color="#000000"]Returns current number of spaces allocated. [color="#000000"]ensure-
[color="#000000"]Capacity( )
[color="#000000"]Integer indicating desired capacity. [color="#000000"]Makes the [color="#000000"]StringBuffer[color="#000000"] hold at least the desired number of spaces. [color="#000000"]setLength( ) [color="#000000"]Integer indicating new length of character string in buffer. [color="#000000"]Truncates or expands the previous character string. If expanding, pads with nulls. [color="#000000"]charAt( ) [color="#000000"]Integer indicating the location of the desired element. [color="#000000"]Returns the [color="#000000"]char[color="#000000"] at that location in the buffer. [color="#000000"]setCharAt( ) [color="#000000"]Integer indicating the location of the desired element and the new [color="#000000"]char[color="#000000"] value for the element. [color="#000000"]Modifies the value at that location. [color="#000000"]getChars( ) [color="#000000"]The beginning and end from which to copy, the array to copy into, an index into the destination array. [color="#000000"]Copy [color="#000000"]char[color="#000000"]s into an external array. There’s no [color="#000000"]getBytes( )[color="#000000"] as in [color="#000000"]String[color="#000000"]. [color="#000000"]append( ) [color="#000000"]Overloaded: [color="#000000"]Object[color="#000000"], [color="#000000"]String[color="#000000"], [color="#000000"]char[][color="#000000"], [color="#000000"]char[][color="#000000"] with offset and length, [color="#000000"]boolean[color="#000000"], [color="#000000"]char[color="#000000"], [color="#000000"]int[color="#000000"], [color="#000000"]long[color="#000000"], [color="#000000"]float[color="#000000"], [color="#000000"]double[color="#000000"]. [color="#000000"]The argument is converted to a string and appended to the end of the current buffer, increasing the buffer if necessary. [color="#000000"]insert( ) [color="#000000"]Overloaded, each with a first argument of the offset at which to start inserting: [color="#000000"]Object[color="#000000"], [color="#000000"]String[color="#000000"], [color="#000000"]char[][color="#000000"], [color="#000000"]boolean[color="#000000"], [color="#000000"]char[color="#000000"], [color="#000000"]int[color="#000000"], [color="#000000"]long[color="#000000"], [color="#000000"]float[color="#000000"], [color="#000000"]double[color="#000000"]. [color="#000000"]The second argument is converted to a string and inserted into the current buffer beginning at the offset. The buffer is increased if necessary. [color="#000000"]reverse( ) [color="#000000"]The order of the characters in the buffer is reversed. [color="#000000"]最常用的方法是 [color="#000000"]append( )[color="#000000"], 编译器用来计算包含有 +[color="#000000"]’和‘ [color="#000000"]+=[color="#000000"]‘ 的[color="#000000"]String[color="#000000"] 表达式[color="#000000"]. [color="#000000"]insert( )[color="#000000"] 有着相近的形式,这两种方法对缓冲区提供重要的操作,而不是创建新的对象. Strings are special [color="#000000"]By now you’ve seen that the [color="#000000"]String[color="#000000"] class is not just another class in Java. There are a lot of special cases in [color="#000000"]String[color="#000000"], not the least of which is that it’s a built-in class and fundamental to Java. Then there’s the fact that a quoted character string is converted to a [color="#000000"]String[color="#000000"] by the compiler and the special overloaded operators [color="#000000"]+[color="#000000"] and [color="#000000"]+=[color="#000000"]. In this chapter you’ve seen the remaining special case: the carefully-built immutability using the companion [color="#000000"]StringBuffer[color="#000000"] and some extra magic in the compiler.

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/8682/showart_38863.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP