范例1:
length(),,capacity(),setlength(),append()的用法:
public class stringbuffer
{
public static void main(String args[])
{
String s1="l love java";
StringBuffer sb1=new StringBuffer();
sb1.setLength(10);
System.out.println("length="+sb1.length());//字符串的长度
System.out.println("capacity="+sb1.capacity());//字符串的容量
sb1.append(s1);//使用append()想里添加内容
System.out.println("stringbuffer="+sb1);
System.out.println("length="+sb1.length());//字符串的长度
System.out.println("capacity="+sb1.capacity());//字符串的容量
}
}
范例2:
CharAt(),setCharAt()的用法:
public class stringbuffer
{
public static void main(String args[])
{
String s1="hello,java";
StringBuffer sb1=new StringBuffer();
sb1.append(s1);
System.out.println("原始字符串");
System.out.println(sb1);
System.out.println("修改后的字符串");
sb1.setCharAt(1,'i');//设计字符串中的字符
sb1.setLength(2);
System.out.println(sb1);
System.out.println(sb1.charAt(1));//获取字符串中的字符
}
}
范例3:
inset(),reverse(),delete(),deleteCharAt(),substring(),replace()的用法
public class stringbuffer
{
public static void main(String args[])
{
String s1="hello,java";
StringBuffer sb=new StringBuffer();
sb.append(s1);
sb.insert(6,"l love ");//inset(int index,String string) index代表插入的索引值
System.out.println(sb);
System.out.println("reverse sb="+sb.reverse());
System.out.println(sb.reverse());//颠倒字符串,翻转字符串
sb.delete(0,6);//delete(int start,int end)//删除start和end 之间的所有字符
System.out.println("delete 0 to 6 个字符:"+sb);
sb.deleteCharAt(0);//deleteCharAt(int index)删除一个index指定的字符,
System.out.println("delete 'l':"+sb);
sb.replace(0,5,"hello");//replace(int start ,int end,String mystring)使用mystring替换start和end 之间的内容
System.out.println("love replace hello :"+sb);
System.out.println(sb.substring(0,5));//sb.substring(0,5);//substring(int start,int end)获取start和end 之间的内容
}
}
范例4:
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.util.EventListener;
public class stringbuffer extends Frame implements ActionListener
{
/**
* Method actionPerformed
*
*
* @param e
*
*/
static stringbuffer frame=new stringbuffer();
static Button B1=new Button("长度");
static Button B2=new Button("容量");
static Button B3=new Button("替换");
static Button B4=new Button("删除");
static Button B5=new Button("反转");
static Button B6=new Button("插入");
static TextField tf1=new TextField("l love java");
static TextField tf2=new TextField(2);
static TextField tf3=new TextField(2);
static TextField tf4=new TextField(20);
static Label l1=new Label("参数1和参数2是当我们删除或替换,插入字符时输入的索引值,替换内容输入替换的内容,插入时只需要输入参数1, ");
static Label l2=new Label("参数1");
static Label l3=new Label("参数2");
static Label l4=new Label("替换/插入内容");
public static void main(String args[])
{
frame.setTitle("stringbuffer test");
frame.setBackground(Color.red);
frame.setSize(1000,1000);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
tf1.setEditable(false);
l1.setForeground(Color.blue);
B1.addActionListener(frame);
B2.addActionListener(frame);
B3.addActionListener(frame);
B4.addActionListener(frame);
B5.addActionListener(frame);
B6.addActionListener(frame);
frame.setLayout(new FlowLayout());
frame.add(B1);
frame.add(B2);
frame.add(B3);
frame.add(B6);
frame.add(B4);
frame.add(B5);
frame.add(tf1);
frame.add(l2);
frame.add(tf2);
frame.add(l3);
frame.add(tf3);
frame.add(l4);
frame.add(tf4);
frame.add(l1);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// TODO: Add your code here
if(e.getActionCommand()=="长度")
{
StringBuffer sb=new StringBuffer();
sb.append(tf1.getText());
int length=sb.length();
l1.setText("字符串的长度为:"+length);
}
else if(e.getActionCommand()=="容量")
{
StringBuffer sb=new StringBuffer();
sb.append(tf1.getText());
int capacity=sb.capacity();
l1.setText("字符串的容量为:"+capacity);
}
else if(e.getActionCommand()=="替换")
{
StringBuffer sb=new StringBuffer();
sb.append(tf1.getText());
int a=Integer.parseInt(tf2.getText());
int b=Integer.parseInt(tf3.getText());
String str=tf4.getText();
sb.replace(a,b,str);
l1.setText("替换"+a+"到"+b+"后的字符串为:"+sb);
}
else if(e.getActionCommand()=="删除")
{
StringBuffer sb=new StringBuffer();
sb.append(tf1.getText());
int a=Integer.parseInt(tf2.getText());
int b=Integer.parseInt(tf3.getText());
sb.delete(a,b);
l1.setText("删除"+a+"到"+b+"之间的内容后的字符为:"+sb);
}
else if(e.getActionCommand()=="反转")
{
StringBuffer sb=new StringBuffer();
sb.append(tf1.getText());
sb.reverse();
l1.setText("反转后的字符串为:"+sb);
}
else if(e.getActionCommand()=="插入")
{
StringBuffer sb=new StringBuffer();
sb.append(tf1.getText());
int a=Integer.parseInt(tf2.getText());
String str=tf4.getText();
sb.insert(a,str);
l1.setText("插入"+a+"后的字符串为:"+sb);
}
}
}
length(),,capacity(),setlength(),append()的用法:
public class stringbuffer
{
public static void main(String args[])
{
String s1="l love java";
StringBuffer sb1=new StringBuffer();
sb1.setLength(10);
System.out.println("length="+sb1.length());//字符串的长度
System.out.println("capacity="+sb1.capacity());//字符串的容量
sb1.append(s1);//使用append()想里添加内容
System.out.println("stringbuffer="+sb1);
System.out.println("length="+sb1.length());//字符串的长度
System.out.println("capacity="+sb1.capacity());//字符串的容量
}
}
范例2:
CharAt(),setCharAt()的用法:
public class stringbuffer
{
public static void main(String args[])
{
String s1="hello,java";
StringBuffer sb1=new StringBuffer();
sb1.append(s1);
System.out.println("原始字符串");
System.out.println(sb1);
System.out.println("修改后的字符串");
sb1.setCharAt(1,'i');//设计字符串中的字符
sb1.setLength(2);
System.out.println(sb1);
System.out.println(sb1.charAt(1));//获取字符串中的字符
}
}
范例3:
inset(),reverse(),delete(),deleteCharAt(),substring(),replace()的用法
public class stringbuffer
{
public static void main(String args[])
{
String s1="hello,java";
StringBuffer sb=new StringBuffer();
sb.append(s1);
sb.insert(6,"l love ");//inset(int index,String string) index代表插入的索引值
System.out.println(sb);
System.out.println("reverse sb="+sb.reverse());
System.out.println(sb.reverse());//颠倒字符串,翻转字符串
sb.delete(0,6);//delete(int start,int end)//删除start和end 之间的所有字符
System.out.println("delete 0 to 6 个字符:"+sb);
sb.deleteCharAt(0);//deleteCharAt(int index)删除一个index指定的字符,
System.out.println("delete 'l':"+sb);
sb.replace(0,5,"hello");//replace(int start ,int end,String mystring)使用mystring替换start和end 之间的内容
System.out.println("love replace hello :"+sb);
System.out.println(sb.substring(0,5));//sb.substring(0,5);//substring(int start,int end)获取start和end 之间的内容
}
}
范例4:
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.util.EventListener;
public class stringbuffer extends Frame implements ActionListener
{
/**
* Method actionPerformed
*
*
* @param e
*
*/
static stringbuffer frame=new stringbuffer();
static Button B1=new Button("长度");
static Button B2=new Button("容量");
static Button B3=new Button("替换");
static Button B4=new Button("删除");
static Button B5=new Button("反转");
static Button B6=new Button("插入");
static TextField tf1=new TextField("l love java");
static TextField tf2=new TextField(2);
static TextField tf3=new TextField(2);
static TextField tf4=new TextField(20);
static Label l1=new Label("参数1和参数2是当我们删除或替换,插入字符时输入的索引值,替换内容输入替换的内容,插入时只需要输入参数1, ");
static Label l2=new Label("参数1");
static Label l3=new Label("参数2");
static Label l4=new Label("替换/插入内容");
public static void main(String args[])
{
frame.setTitle("stringbuffer test");
frame.setBackground(Color.red);
frame.setSize(1000,1000);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
tf1.setEditable(false);
l1.setForeground(Color.blue);
B1.addActionListener(frame);
B2.addActionListener(frame);
B3.addActionListener(frame);
B4.addActionListener(frame);
B5.addActionListener(frame);
B6.addActionListener(frame);
frame.setLayout(new FlowLayout());
frame.add(B1);
frame.add(B2);
frame.add(B3);
frame.add(B6);
frame.add(B4);
frame.add(B5);
frame.add(tf1);
frame.add(l2);
frame.add(tf2);
frame.add(l3);
frame.add(tf3);
frame.add(l4);
frame.add(tf4);
frame.add(l1);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// TODO: Add your code here
if(e.getActionCommand()=="长度")
{
StringBuffer sb=new StringBuffer();
sb.append(tf1.getText());
int length=sb.length();
l1.setText("字符串的长度为:"+length);
}
else if(e.getActionCommand()=="容量")
{
StringBuffer sb=new StringBuffer();
sb.append(tf1.getText());
int capacity=sb.capacity();
l1.setText("字符串的容量为:"+capacity);
}
else if(e.getActionCommand()=="替换")
{
StringBuffer sb=new StringBuffer();
sb.append(tf1.getText());
int a=Integer.parseInt(tf2.getText());
int b=Integer.parseInt(tf3.getText());
String str=tf4.getText();
sb.replace(a,b,str);
l1.setText("替换"+a+"到"+b+"后的字符串为:"+sb);
}
else if(e.getActionCommand()=="删除")
{
StringBuffer sb=new StringBuffer();
sb.append(tf1.getText());
int a=Integer.parseInt(tf2.getText());
int b=Integer.parseInt(tf3.getText());
sb.delete(a,b);
l1.setText("删除"+a+"到"+b+"之间的内容后的字符为:"+sb);
}
else if(e.getActionCommand()=="反转")
{
StringBuffer sb=new StringBuffer();
sb.append(tf1.getText());
sb.reverse();
l1.setText("反转后的字符串为:"+sb);
}
else if(e.getActionCommand()=="插入")
{
StringBuffer sb=new StringBuffer();
sb.append(tf1.getText());
int a=Integer.parseInt(tf2.getText());
String str=tf4.getText();
sb.insert(a,str);
l1.setText("插入"+a+"后的字符串为:"+sb);
}
}
}