如何在字符串中提取一段字符串

时间:2015-11-22 23:00:30

标签: excel excel-formula

我有一个日志,我想在“name =”之后提取任何内容。日志看起来像这样:

BC_WVM_FAIL: Rend wrong size for BCID 4608603 rend_id: 4608621 expected sz: 220459951 actual sz: 90816512 name: The Real Housewives Of Orange County - S10 Ep2

1 个答案:

答案 0 :(得分:0)

你在这里有几个选择。两者都围绕使用FIND function(区分大小写)或 SEARCH function(不区分大小写),用于查找拼接出较长文本字符串的内容的起始位置。

import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;

public class TestPDF {

    public static void main(String[] args) {

        Vector columnNames = new Vector();
        columnNames.add("Day  / Lecture  -->>");
        columnNames.add("First");
        columnNames.add("Second");
        columnNames.add("Third");
        columnNames.add("Fourth");
        columnNames.add("Fifth");

        columnNames.add("Sixth");
        columnNames.add("Seventh");
        columnNames.add("Eighth");

        Vector data = new Vector();
        data.add(makeRow(new String[]{"Monday", "LAB", "", "", "", "CS rashmi", "STOA", "STOA", ".NET"}));
        data.add(makeRow(new String[]{"Tuesday", ".NET", ".NET", "SC", "SC", "ADBMS", "ADBMS", "CS", "STOA"}));
        data.add(makeRow(new String[]{"Wednesday", "ADBMS", "ADBMS", "SC", "SC", ".NET", "CS", "CS", "STOA"}));
        data.add(makeRow(new String[]{"Thursday", "LAB .Net", "", "", "", "ADBMS P", ".NET", "CS", "STOA"}));
        data.add(makeRow(new String[]{"Friday", "LAV ADBMS", "", "", "", "LAB .NET", "", "", ""}));

        JTable tab = new JTable(data, columnNames);

//        for (int i = 1; i <= 8; i++) {
//            TableColumn column = tab.getColumnModel().getColumn(i);
//            column.setPreferredWidth(5);
//        }
//
//        tab.setEnabled(false);
//
        tab.setBorder(BorderFactory.createLineBorder(Color.green, 3));
        tab.setFont(new Font("Arial", Font.PLAIN, 15));
        tab.setRowHeight(30);
        tab.setGridColor(Color.red);

        JTableHeader head = tab.getTableHeader();
        head.setFont(new Font("SansSerif", Font.ITALIC, 15));
        head.setForeground(Color.magenta);

        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment(SwingConstants.CENTER);
        tab.setDefaultRenderer(String.class, centerRenderer);

//        JFrame frame = new JFrame();
//        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//        frame.add(new JScrollPane(tab));
//        frame.pack();
//        frame.setLocationRelativeTo(null);
//        frame.setVisible(true);
//        JOptionPane.showMessageDialog(null, new JScrollPane(tab));
        Document document = new Document(PageSize.A4.rotate());
        try {

            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("Test.pdf"));

            document.open();
            PdfContentByte cb = writer.getDirectContent();

            cb.saveState();
            Graphics2D g2 = cb.createGraphicsShapes(1600, 500);

            Shape oldClip = g2.getClip();
            g2.clipRect(0, 0, 1600, 1000);

            Rectangle pageSize = document.getPageSize();
            float width = pageSize.getWidth();
            float height = pageSize.getHeight();

            // Size the JTable to be the width of the page, but maintain
            // it's own overall height
            tab.setSize((int) width, tab.getPreferredSize().height);

            JTableHeader tableHeader = tab.getTableHeader();
            tableHeader.setSize((int) width, tableHeader.getPreferredSize().height);

            // Get the table's printable
            Printable printable = tab.getPrintable(JTable.PrintMode.FIT_WIDTH, null, null);

            // Create a Paper which meets the size of the available document...
            Paper paper = new Paper();
            paper.setSize(height, width);
            paper.setImageableArea(0, 0, height, width);
            // Create a PageFormat for the Paper
            PageFormat pageFormat = new PageFormat();
            pageFormat.setOrientation(PageFormat.LANDSCAPE);
            pageFormat.setPaper(paper);
            // Print each page of the table
            int pageIndex = 0;
            while (printable.print(g2, pageFormat, pageIndex) != Printable.NO_SUCH_PAGE) {
                System.out.println("Print " + pageIndex);
                printable.print(g2, pageFormat, pageIndex);
                pageIndex++;
            }
            g2.setClip(oldClip);

            g2.dispose();
            cb.restoreState();
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        document.close();
    }

    protected static Vector makeRow(String[] values) {
        return new Vector(Arrays.asList(values));
    }

}