Example usage for java.lang String length

List of usage examples for java.lang String length

Introduction

In this page you can find the example usage for java.lang String length.

Prototype

public int length() 

Source Link

Document

Returns the length of this string.

Usage

From source file:ClipBoardCopyPaster.java

public static void main(String[] args) {
    Display display = new Display();
    final Clipboard cb = new Clipboard(display);
    final Shell shell = new Shell(display);

    final Text text = new Text(shell, SWT.BORDER | SWT.SINGLE);
    text.setBounds(5, 5, 100, 20);/*  w  w w . j a v a  2s.  c om*/

    Button copy = new Button(shell, SWT.PUSH);
    copy.setBounds(5, 50, 100, 20);
    copy.setText("Copy");
    copy.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            String textData = text.getSelectionText();
            if (textData.length() > 0) {
                TextTransfer textTransfer = TextTransfer.getInstance();
                cb.setContents(new Object[] { textData }, new Transfer[] { textTransfer });
            }
        }
    });

    Button paste = new Button(shell, SWT.PUSH);
    paste.setBounds(5, 90, 100, 20);
    paste.setText("Paste");
    paste.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            TextTransfer transfer = TextTransfer.getInstance();
            String data = (String) cb.getContents(transfer);
            if (data != null) {
                text.insert(data);
            }
        }
    });

    shell.setSize(200, 200);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    cb.dispose();
    display.dispose();
}

From source file:RegexTable.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Regexing JTable");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Object rows[][] = { { "A", "About", 44.36 }, { "B", "Boy", 44.84 }, { "C", "Cat", 463.63 },
            { "D", "Day", 27.14 }, { "E", "Eat", 44.57 }, { "F", "Fail", 23.15 }, { "G", "Good", 4.40 },
            { "H", "Hot", 24.96 }, { "I", "Ivey", 5.45 }, { "J", "Jack", 49.54 }, { "K", "Kids", 280.00 } };
    String columns[] = { "Symbol", "Name", "Price" };
    TableModel model = new DefaultTableModel(rows, columns) {
        public Class getColumnClass(int column) {
            Class returnValue;/*from  www . j  a  v a2 s.c o m*/
            if ((column >= 0) && (column < getColumnCount())) {
                returnValue = getValueAt(0, column).getClass();
            } else {
                returnValue = Object.class;
            }
            return returnValue;
        }
    };

    final JTable table = new JTable(model);
    final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
    table.setRowSorter(sorter);
    JScrollPane pane = new JScrollPane(table);
    frame.add(pane, BorderLayout.CENTER);

    JPanel panel = new JPanel(new BorderLayout());
    JLabel label = new JLabel("Filter");
    panel.add(label, BorderLayout.WEST);
    final JTextField filterText = new JTextField("A");
    panel.add(filterText, BorderLayout.CENTER);
    frame.add(panel, BorderLayout.NORTH);
    JButton button = new JButton("Filter");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String text = filterText.getText();
            if (text.length() == 0) {
                sorter.setRowFilter(null);
            } else {
                sorter.setRowFilter(RowFilter.regexFilter(text));
            }
        }
    });
    frame.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 250);
    frame.setVisible(true);
}

From source file:OldStyle.java

public static void main(String args[]) {
    ArrayList list = new ArrayList();

    // These lines store strings, but any type of object 
    // can be stored.  In old-style code, there is no  
    // convenient way restrict the type of objects stored 
    // in a collection 
    list.add("one");
    list.add("two");
    list.add("three");
    list.add("four");

    Iterator itr = list.iterator();
    while (itr.hasNext()) {

        // To retrieve an element, an explicit type cast is needed 
        // because the collection stores only Object. 
        String str = (String) itr.next(); // explicit cast needed here. 

        System.out.println(str + " is " + str.length() + " chars long.");
    }/*from   ww w .  j  a va  2s.c  o m*/
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    ServerSocket ss = new ServerSocket(80);
    while (true) {
        Socket s = ss.accept();//from  w  w  w  . j a  v  a  2s  .co  m
        PrintStream out = new PrintStream(s.getOutputStream());
        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String info = null;
        while ((info = in.readLine()) != null) {
            System.out.println("now got " + info);
            if (info.equals(""))
                break;
        }
        out.println("HTTP/1.0 200 OK");
        out.println("MIME_version:1.0");
        out.println("Content_Type:text/html");
        String c = "<html> <head></head><body> <h1> hi</h1></Body></html>";
        out.println("Content_Length:" + c.length());
        out.println("");
        out.println(c);
        out.close();
        s.close();
        in.close();
    }
}

From source file:MainClass.java

public static void main(String[] args) {
    long[] primes = new long[] { 1, 2, 3, 5, 7 };
    File aFile = new File("C:/test/primes.txt");
    FileOutputStream outputFile = null;
    try {/*w w  w  .  j a va 2  s.  co m*/
        outputFile = new FileOutputStream(aFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    FileChannel file = outputFile.getChannel();
    final int BUFFERSIZE = 100;
    ByteBuffer buf = ByteBuffer.allocate(BUFFERSIZE);
    DoubleBuffer doubleBuf = buf.asDoubleBuffer();
    buf.position(8);
    CharBuffer charBuf = buf.asCharBuffer();
    for (long prime : primes) {
        String primeStr = "prime = " + prime;
        doubleBuf.put(0, (double) primeStr.length());
        charBuf.put(primeStr);
        buf.position(2 * charBuf.position() + 8);
        LongBuffer longBuf = buf.asLongBuffer();
        longBuf.put(prime);
        buf.position(buf.position() + 8);
        buf.flip();
        try {
            file.write(buf);
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }
        buf.clear();
        doubleBuf.clear();
        charBuf.clear();
    }
    try {
        System.out.println("File written is " + file.size() + "bytes.");
        outputFile.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:com.sm.test.TestError.java

public static void main(String[] args) throws Exception {
    String[] opts = new String[] { "-configPath", "-url", "-store", "-times" };
    String[] defaults = new String[] { "", "localhost:6172", "store1", "2" };
    String[] paras = getOpts(args, opts, defaults);

    String configPath = paras[0];
    if (configPath.length() == 0) {
        logger.error("missing config path or host");
        throw new RuntimeException("missing -configPath or -host");
    }//from  w w w . ja  va  2  s.  c  o  m

    String url = paras[1];
    String store = paras[2];
    int times = Integer.valueOf(paras[3]);
    ClusterClientFactory ccf = ClusterClientFactory.connect(url, store);
    ClusterClient client = ccf.getDefaultStore(8000L);
    TestError testClient = new TestError(client);
    for (int i = 0; i < times; i++) {
        try {
            Key key = Key.createKey("test" + i);
            //client.put(key, "times-" + i);
            Value value = client.get(key);
            value.setVersion(value.getVersion() - 2);
            client.put(key, value);
            logger.info(value == null ? "null" : value.getData().toString());
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
    }
    testClient.client.close();
    ccf.close();
}

From source file:MainClass.java

public static void main(String args[]) {
    String s1 = "hello there";
    char charArray[] = new char[5];

    System.out.printf("s1: %s", s1);

    for (int count = s1.length() - 1; count >= 0; count--)
        System.out.printf("%s ", s1.charAt(count));

    // copy characters from string into charArray
    s1.getChars(0, 5, charArray, 0);/* ww w  .j  a v  a 2s  .  c o m*/
    System.out.print("\nThe character array is: ");

    for (char character : charArray)
        System.out.print(character);
}

From source file:ComputeInitials.java

public static void main(String[] args) {
    String myName = "Fred F. Flintstone";
    StringBuffer myInitials = new StringBuffer();
    int length = myName.length();

    for (int i = 0; i < length; i++) {
        if (Character.isUpperCase(myName.charAt(i))) {
            myInitials.append(myName.charAt(i));
        }/*w  w w .  j  av  a2  s.c o m*/
    }
    System.out.println("My initials are: " + myInitials);
}

From source file:fr.mycellar.tools.MyCellarPasswordGenerator.java

public static void main(String... args) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
            ApplicationConfiguration.class);
    StringPasswordEncryptor encryptor = new StringPasswordEncryptor();
    encryptor.setStringDigester(context.getBean(StringDigester.class));
    String encryptedPassword = encryptor.encryptPassword("test");
    System.out.println(encryptedPassword + " size: " + encryptedPassword.length());
}

From source file:com.mycompany.mavenproject1.Ex1.java

public static void main(String[] args) {
    Ex1 obj = new Ex1();
    String data = obj.getFileWithUtil("data.json");
    for (int i = 0; i < data.length(); i++) {
        System.out.println(Integer.toBinaryString(data.charAt(i)));
    }//  ww  w .ja  va 2  s . co  m
}