根据字段02对字符串进行分行

案例如下:

在下面的文本框中输入字符串段落,其中要包括(02)字段,然后单击“分行显示”按钮,程序将根据(02)字段对字符串进行分行。

     本实例重点是使用StringBuilder便捷、高效地操作字符串。

1)追加字符串

   构建器的append()方法,可以向其尾部追加新的字符串,该方法的声明为:public StringBuilder append(String str)  

   参数说明

   str:要向构造器尾部追加的字符串。

2)生成字符串

   构建器是StringBuilder类的对象,要像String类一样应用其内容,需要通过toStirng()方法转换为字符串对象,该方法的声明为:public String toString()

   该方法把构建器中的所有内容串联成一个不可变的字符串对象,然后,供其他业务代码使用。

 

package cn.ch.da;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.border.TitledBorder;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.UIManager;

public class StringLinewrap extends JFrame {
    
    private JPanel contentPane;
    private JTextArea sourceTextArea;
    private JTextArea destinationTextArea;
    
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            UIManager
                    .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Throwable e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    StringLinewrap frame = new StringLinewrap();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    /**
     * Create the frame.
     */
    public StringLinewrap() {
        setTitle("根据字段02对字符串进行分行");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 475, 441);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        JLabel label = new JLabel(
                "<html>在下面的文本框中输入字符串段落,其中要包括(02)字段,然后单击“分行显示”按钮,程序将根据(02)字段进行分行。</html>");
        label.setBorder(new TitledBorder(null, "说明",
                TitledBorder.LEADING, TitledBorder.TOP, null, null));
        label.setBounds(10, 10, 439, 76);
        contentPane.add(label);
        
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 84, 439, 98);
        contentPane.add(scrollPane);
        
        sourceTextArea = new JTextArea();
        sourceTextArea.setLineWrap(true);
        scrollPane.setViewportView(sourceTextArea);
        
        JButton button = new JButton("分行显示");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_button_actionPerformed(e);
            }
        });
        button.setBounds(177, 192, 101, 25);
        contentPane.add(button);
        
        JScrollPane scrollPane_1 = new JScrollPane();
        scrollPane_1.setBounds(10, 233, 439, 156);
        contentPane.add(scrollPane_1);
        
        destinationTextArea = new JTextArea();
        scrollPane_1.setViewportView(destinationTextArea);
        
    }
    
    protected void do_button_actionPerformed(ActionEvent e) {
        String sourceString = sourceTextArea.getText();// 获取用户输入字段
        String[] lines = sourceString.split("02");// 根据“02字段”分割字符串为数组
        StringBuilder sbuilder = new StringBuilder();// 创建字符串构建器
        for (String line : lines) {// 遍历分割后的字符串数组
            // 把每个数组元素的字符串与回车符相连并添加到字符串构建器中
            sbuilder.append("02"+line + "\n");
        }
        // 把字符串添加到换行显示字符串的文本域中
        destinationTextArea.setText(sbuilder.toString());
    }
}


效果如下:

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sanqima

一键三连,多多益善

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值