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:AnagramApp.java

public static void main(String[] args) throws IOException {
    String input = "Java Source and Support";
    size = input.length();
    count = 0;//  w w  w .j  av a  2s.  co m
    charArray = new char[size];
    for (int j = 0; j < size; j++)
        charArray[j] = input.charAt(j);
    doAnagram(size);
}

From source file:StringsDemo.java

public static void main(String[] args) {
    String palindrome = "Dot saw I was Tod";
    int len = palindrome.length();
    StringBuffer dest = new StringBuffer(len);

    for (int i = (len - 1); i >= 0; i--) {
        dest.append(palindrome.charAt(i));
    }//from  w  w w  .  j a va2  s .  co  m
    System.out.println(dest.toString());
}

From source file:CharArrayWriterDemo.java

public static void main(String args[]) throws IOException {
    CharArrayWriter f = new CharArrayWriter();
    String s = "This should end up in the array";
    char buf[] = new char[s.length()];
    s.getChars(0, s.length(), buf, 0);//from  w  ww. j av  a  2 s. com
    f.write(buf);
    System.out.println(f.toString());

    char c[] = f.toCharArray();
    for (int i = 0; i < c.length; i++) {
        System.out.print(c[i]);
    }

    FileWriter f2 = new FileWriter("test.txt");
    f.writeTo(f2);
    f2.close();
    f.reset();
    for (int i = 0; i < 3; i++)
        f.write('X');
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String bubba = "this is a test this is a test";
    Map<Integer, Integer> occurrences = new HashMap<Integer, Integer>();

    for (String currentWord : bubba.split(" ")) {
        Integer current = occurrences.get(currentWord.length());
        if (current == null) {
            current = 0;//from   www.ja v a 2s.c  o  m
        }
        occurrences.put(currentWord.length(), current + 1);
    }
    for (Integer currentKey : occurrences.keySet()) {
        System.out.println("There are " + occurrences.get(currentKey) + " " + currentKey + " letter words");
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("MultiHighlight");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextArea comp = new JTextArea(5, 20);
    comp.setText("this is a test");
    frame.getContentPane().add(new JScrollPane(comp), BorderLayout.CENTER);

    String charsToHighlight = "a";
    Highlighter h = comp.getHighlighter();
    h.removeAllHighlights();//from  ww w .j  a  v  a 2 s. c  om
    String text = comp.getText().toUpperCase();

    for (int j = 0; j < text.length(); j += 1) {
        char ch = text.charAt(j);
        if (charsToHighlight.indexOf(ch) >= 0)
            h.addHighlight(j, j + 1, DefaultHighlighter.DefaultPainter);
    }
    frame.pack();
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextField nameTextField = new JTextField();
    frame.add(nameTextField, BorderLayout.NORTH);
    frame.add(new JTextField(), BorderLayout.SOUTH);

    InputVerifier verifier = new InputVerifier() {
        public boolean verify(JComponent input) {
            final JTextComponent source = (JTextComponent) input;
            String text = source.getText();
            if ((text.length() != 0) && !(text.equals("Exit"))) {
                JOptionPane.showMessageDialog(source, "Can't leave.", "Error Dialog",
                        JOptionPane.ERROR_MESSAGE);
                return false;
            } else {
                return true;
            }/*from  w ww  .j a v a2  s . c  o m*/
        }
    };
    nameTextField.setInputVerifier(verifier);

    frame.setSize(250, 100);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    byte buff[] = new byte[1024];
    InetAddress addr = InetAddress.getByName("www.java2s.com");
    Socket s = new Socket(addr, 80);

    OutputStream output = s.getOutputStream();
    InputStream input = s.getInputStream();
    String GetCmd = "GET /index.html HTTP/1.0\r\n\r\n";
    GetCmd.getBytes(0, GetCmd.length(), buff, 0);
    output.write(buff);//  w w w  .ja  v  a2 s . com
    input.read(buff, 0, buff.length);
    System.out.println(new String(buff, 0));

}

From source file:Main.java

public static void main(String[] args) {
    String r = "This is a test.";
    String[] s = new String[10];
    int len = r.length();
    int l = 3;/*  w w w.  java  2  s.c  om*/
    int last;
    int f = 0;
    for (int i = 0;; i++) {
        last = (f + l);
        if ((last) >= len) {
            last = len;
        }
        s[i] = r.substring(f, last);
        System.out.println(s[i]);

        if (last == len) {
            break;
        }
        f = (f + l);
    }
}

From source file:NewStyle.java

public static void main(String args[]) {

    ArrayList<String> list = new ArrayList<String>();

    list.add("one");
    list.add("two");
    list.add("three");
    list.add("four");

    Iterator<String> itr = list.iterator();

    while (itr.hasNext()) {
        String str = itr.next(); // no cast needed

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

From source file:Main.java

public static void main(String[] args) {
    int count = 1;
    String userInput = "this is a test";
    char ch = userInput.charAt(0);
    for (int i = 1; i < userInput.length(); i++) {
        if (userInput.charAt(i) == ch)
            count++;/*w  w  w .ja va  2s .  c o m*/
    }
    System.out.println(count);

}