Java StringBuffer Class - Java 147

Java StringBuffer Class – Java 147

Java StringBuffer Class

提供了可變的 StringBuffer 類別,調用這些方法可以直接修改值,而不必建立新的物件,用以替代 String, StringBuffer Class 介紹常見的 charAt 、 indexOf 、 lastIndexOf 、 substring 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- string
       |                   +- stringbuffer
       |                       +- StringBufferClassTest.java   

單元測試

提供查找、搜尋、取代等操作 String 。

charAt

建立一個 StringBuffer ,增加字串後,取得指定索引的字元。

	@Test
	public void charAt() {
		char expected = 'g';
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		char result = builder.charAt(5);
		System.out.println(result);
		assertEquals(expected, result);
		
		result = builder.charAt(7);
		System.out.println(result);
		assertEquals('b', result);
	}
g
b

getChars

StringBuffer Class Java 建立一個 StringBuffer ,增加字串後,取得字元。

	@Test
	public void getChars() {
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		IntStream stream = builder.chars();
		List<Integer> list = stream.boxed().collect(Collectors.toList());
		System.out.println(list);
	}
[83, 112, 114, 105, 110, 103, 32, 98, 111, 111, 116, 32, 49, 54, 56]

indexOf

StringBuffer Class Java 建立一個 StringBuffer ,增加字串後,搜尋指定的字串,傳回位置。

	@Test
	public void indexOf() {
		int expected = 3;
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		int result = builder.indexOf("i");
		System.out.println(result);
		assertEquals(expected, result);

		result = builder.indexOf("i", 2);
		System.out.println(result);
		assertEquals(3, result);
	}
3
3

lastIndexOf

StringBuffer Class Java 建立一個 StringBuffer ,增加字串後,從後面搜尋指定的字串,傳回位置。

	@Test
	public void lastIndexOf() {
		int expected = 3;
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		int result = builder.lastIndexOf("i");
		System.out.println(result);
		assertEquals(expected, result);

		result = builder.lastIndexOf("i", 5);
		System.out.println(result);
		assertEquals(3, result);
	}
3
3

setCharAt

StringBuffer Class in Java 建立一個 StringBuffer ,增加字串後,設定指定位置的字元。

	@Test
	public void setCharAt() {
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		builder.setCharAt(3, 'I');
		System.out.println(builder);
		assertEquals("SprIng boot 168", builder.toString());
	}
SprIng boot 168

substring

StringBuffer Class in Java 建立一個 StringBuffer 增加字串後,取得指定位置的字串。

	@Test
	public void substring() {
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		String result = builder.substring(5);
		System.out.println(result);
		assertEquals("g boot 168", result);

		result = builder.substring(0, 5);
		System.out.println(result);
		assertEquals("Sprin", result);
	}
g boot 168
Sprin

replace

StringBuffer Class in Java 建立一個 StringBuffer ,增加字串後,取代指定位置的字串。

	@Test
	public void replace() {
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		builder.replace(0, 4, "SPRING");
		System.out.println(builder);
		assertEquals("SPRINGng boot 168", builder.toString());
	}
SPRINGng boot 168

StringBufferClassTest.java

StringBuffer Class in Java 新增單元測試,驗證是否符合預期。

package org.ruoxue.java_147.string.stringbuffer;

import static org.junit.Assert.*;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.junit.Test;

public class StringBufferClassTest {

	@Test
	public void charAt() {
		char expected = 'g';
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		char result = builder.charAt(5);
		System.out.println(result);
		assertEquals(expected, result);
		
		result = builder.charAt(7);
		System.out.println(result);
		assertEquals('b', result);
	}

	@Test
	public void getChars() {
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		IntStream stream = builder.chars();
		List<Integer> list = stream.boxed().collect(Collectors.toList());
		System.out.println(list);
	}

	@Test
	public void indexOf() {
		int expected = 3;
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		int result = builder.indexOf("i");
		System.out.println(result);
		assertEquals(expected, result);

		result = builder.indexOf("i", 2);
		System.out.println(result);
		assertEquals(3, result);
	}

	@Test
	public void lastIndexOf() {
		int expected = 3;
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		int result = builder.lastIndexOf("i");
		System.out.println(result);
		assertEquals(expected, result);

		result = builder.lastIndexOf("i", 5);
		System.out.println(result);
		assertEquals(3, result);
	}

	@Test
	public void setCharAt() {
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		builder.setCharAt(3, 'I');
		System.out.println(builder);
		assertEquals("SprIng boot 168", builder.toString());
	}

	@Test
	public void substring() {
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		String result = builder.substring(5);
		System.out.println(result);
		assertEquals("g boot 168", result);

		result = builder.substring(0, 5);
		System.out.println(result);
		assertEquals("Sprin", result);
	}

	@Test
	public void replace() {
		StringBuffer builder = new StringBuffer();
		builder.append("Spring boot 168");
		builder.replace(0, 4, "SPRING");
		System.out.println(builder);
		assertEquals("SPRINGng boot 168", builder.toString());
	}
}

心得分享

Java StringBuffer Class Example 由於 Java 中的 String 類別建立了一個不可變的字串序列,使用 StringBuffer 用來替代 String 的操作, StringBuffer Class in Java 提供 charAt 、 indexOf 、 lastIndexOf 、 substring 等操作範例。

發佈留言