package com.bjsxt.builder;
/**
-
StringBuilder的使用
-
最常用的操作
-
StringBuilder builder = new StringBuilder(“中华人民共和国”);// String—>StringBuilder
-
builder.append(“北京市”);
-
String str = builder.toString(); //StringBuilder—>String
-
使用场合是什么?SQL语句的拼接
-
StringBuilder builder = new StringBuilder(" select * from job where 1= 1 ");
-
if(sal>10000){
-
builder.append(" and sal >"+sal);
-
}
-
if(site != null){
-
builder.append(" and site ="+site);
-
}
-
if(position != null){
-
builder.append(" and position = "+ position);
-
}
-
String sql = builder.toString()
*/
public class TestStringBuilder {
public static void main(String[] args) {
//1.创建一个StringBuilder对象
//StringBuilder builder = new StringBuilder();
StringBuilder builder = new StringBuilder(“中华人民共和国”);//2.对StringBuilder进行操作//2.1 末尾追加 builder.append("北京市"); builder.append("大兴区"); builder.append("亦庄开发区"); builder.append("北京尚学堂");//2.2 中间添加 builder.insert(7,"华北地区");//2.4 删除 builder.delete(7,11); builder.deleteCharAt(12);//2.3 修改操作
builder.replace(0, 7, “中国”);
//2.5 反转 //builder.reverse();//2.6 StringBuilder--->String String str = builder.toString(); System.out.println(str);
}
}