---
title: "String和StringBuffer的规则和应用"
author: "aaronjiu_00"
date: "2008-11-21T19:07:36Z"
tags: ["C", "C#", "C++", "Eclipse", "编程"]
categories: ["编程语言"]
slug: "string-stringbuffer"
draft: false
lastmod: "2008-12-12T13:07:02Z"
id: 273320
url: "https://tonglin.iteye.com/blog/273320"
---

在Java编程语言中，大家接触最多的，莫过于String和StringBuffer类了吧，这两个类简单到你不屑一顾，但是你在编码实践过程中真正用好它们了吗？下面是我在代码Review过程中总结出来的若干点，和大家分享一下：

- 避免重复文字  
  包含重复的字符串字面的代码通常可以将字符串声明为一个Constant Field得到改善，如果你使用Eclipse，请选中字符串"hello"，然后选中菜单“Refactor-&gt;Extract Constant...”，OK  
  例如：
  
  ```
  public class SomeClass {
  
  	public void someMethod() {
  		someSubMethod("hello");
  		someSubMethod2("hello");
  		someSubMethod3("hello");
  		someSubMethod4("hello");
  	}
  
  	private void someSubMethod(String string) {
  		// TODO Auto-generated method stub
  	}
  
  	private void someSubMethod2(String string) {
  		// TODO Auto-generated method stub
  	}
  
  	private void someSubMethod3(String string) {
  		// TODO Auto-generated method stub
  	}
  
  	private void someSubMethod4(String string) {
  		// TODO Auto-generated method stub
  	}
  
  }
  ```
- 字符串实例化问题  
  避免使用new关键字实例化字符串对象  
  例如：
  
  ```
  public class SomeClass {
  
  	private String someField = new String("something");// 使用String someField = "something"替换
  
  }
  ```
- 不必调用String的toString()方法  
  避免调用字符串对象的toString()方法  
  例如：
  
  ```
  public class SomeClass {
  
  	public String someMethod() {
  		String someStr = "hello";
  		return someStr.toString();// 去掉toString()
  	}
  
  }
  ```
- 不要使用低效的字符串追加  
  避免在StringBuffer的构造函数或append()方法中连接非文字字符串串。  
  例如：
  
  ```
  public class SomeClass {
  
  	public void someMethod() {
  		// 避免这种情况
  		StringBuffer sb = new StringBuffer("tmp = " + System.getProperty("java.io.tmpdir"));
  
  		// 像这样使用
  		StringBuffer sb = new StringBuffer("tmp = ");
  		sb.append(System.getProperty("java.io.tmpdir"));
  	}
  
  }
  ```
- 不必要大小写转换  
  使用equalsIgnoreCase()的速度比使用toUpperCase/toLowerCase().equals()快得多  
  例如：
  
  ```
  public class SomeClass {
  
  	public boolean someMethod(String arg) {
  		// 应该是 "hello".equalsIgnoreCase(arg)
  		return arg.toUpperCase().equals("hello");
  		// 另一个不必要的 toUpperCase()
  		return arg.toUpperCase().equalsIgnoreCase("hello");
  	}
  
  }
  ```
- 使用StringBuffer的length()方法  
  使用StringBuffer.length()来判断StringBuffer的长度，而不是使用StringBuffer.toString().equals("")或StringBuffer.toString().length() == 来判断  
  例如：
  
  ```
  public class SomeClass {
  
  	void someMethod() {
  		StringBuffer sb = new StringBuffer();
  		// 这么用不好
  		if (sb.toString().equals("")) {
  		}
  		// 这么用好
  		if (sb.length() == 0) {
  		}
  	}
  
  }
  ```
- 使用char参数追加单个字符  
  避免在StringBuffer.append()方法中将单个字符char做为字符串String连接。  
  例如：
  
  ```
  public class SomeClass {
  
  	void someMethod() {
  		StringBuffer sb = new StringBuffer();
  		// 避免这种情况
  		sb.append("a");
  
  		// 像这样使用
  		StringBuffer sb = new StringBuffer();
  		sb.append('a');
  	}
  
  }
  ```
- 不要连续地追加字面字符串  
  连续地调用StringBuffer.append追加String文字  
  例如：
  
  ```
  public class SomeClass {
  
  	void someMethod() {
  		StringBuffer buf = new StringBuffer();
  		buf.append("Hello").append(" ").append("World"); // 不好
  		buf.append("Hello World"); // 好
  	}
  
  }
  ```
- 使用indexOf(char c)  
  当检查单一字符char在字符串中的位置时使用String.indexOf(char)，它执行得更快。  
  例如：
  
  ```
  public class SomeClass {
  
  	void someMethod() {
  		String s = "hello world";
  		// 避免这种情况
  		if (s.indexOf("d") != -1) {
  
  		}
  		// 这样较好
  		if (s.indexOf('d') != -1) {
  		}
  	}
  	
  }
  ```
- 低效的检查空字符串  
  String.trim().length()是一种低效的检查字符串为空白blank的方式，因为它创建了一个新的String对象只是为了检查其大小。考虑创建一个静态函数，循环遍历这个字符串，对每个字符检查Character.isWhitespace()，如果发现一个非空白字符，并返回false；其实你可以使用commons-lang包的StringUtils.isBlank()、StringUtils.isNotBlank()、StringUtils.isEmpty()、StringUtils.isNotEmpty()等一系列工具方法。  
  例如： 
  
  ```
  public class SomeClass {
  
  	void someMethod(String string) {
  		if (string != null && string.trim().length() > 0) {
  			doSomething();
  		}
  	}
  
  	private void doSomething() {
  		// TODO Auto-generated method stub
  	}
  
  }
  ```
- 声明StringBuffer时预设大小不正确  
  声明StringBuffer对象时预设大小不正确，可能会导致它在运行时多次重新计算大小。一个无参StringBuffer构造函数以16个字符初始化对象。  
  例如：
  
  ```
  public class SomeClass {
  
  	void someMethod() {
  		StringBuffer bad = new StringBuffer();
  		bad.append("This is a long string, will exceed the default 16 characters");// 不好
  		StringBuffer good = new StringBuffer(41);
  		good.append("This is a long string, which is pre-sized");// 好
  	}
  
  }
  ```
- 无用的字符串ValueOf  
  没有必要调用String.valueOf()将一个整形int等连接到一个字符串；只需直接地使用valueOf()参数。  
  例如：
  
  ```
  public class SomeClass {
  
  	public String convert(int i) {
  		String s;
  		s = "a" + String.valueOf(i); // 不好
  		s = "a" + i; // 更好
  		return s;
  	}
  
  }
  ```
- 不要使用字符参数构造StringBuffer  
  StringBuffer sb = new StringBuffer('c'); 字符char将转换成整数int，以初始化StringBuffer的大小。  
  例如：
  
  ```
  public class SomeClass {
  
  	StringBuffer sb1 = new StringBuffer('c'); // 不好
  	StringBuffer sb2 = new StringBuffer("c"); // 较好
  
  }
  ```
- 使用equalsTo()比较字符串  
  用' == '或'!= '是比较字符串的引用，使用equalsTo()比较字符串实际值  
  例如：
  
  ```
  public class SomeClass {
  
  	boolean test(String s) {
  		if (s == "one")
  			return true; // 不好
  		if ("two".equals(s))
  			return true; // 比较好
  		return false;
  	}
  
  }
  ```
- 避免将StringBuffer作为实例变量  
  StringBuffers可能会增长很快，因此可能成为一个内存泄漏之源（如果所属的类具有较长的生命周期） 。  
  例如：
  
  ```
  public class SomeClass {
  
  	private StringBuffer memoryLeak;
  
  }
  ```