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

public static void main(String[] args) {
    System.out.println("Enter a string");
    Scanner input = new Scanner(System.in);
    String s1 = input.nextLine();
    s1 = s1.trim();/*from   w ww . ja  v  a 2 s  .c  o  m*/
    int howLong = s1.length();

    for (int counter = 0; counter < howLong; counter++) {
        char ch = s1.charAt(counter);
        System.out.print(ch);
    }

}

From source file:Main.java

public static void main(String[] args) {
    String str = "JAVA2S.COM";

    // Get the length of string
    int len = str.length();

    // Loop through all characters and print their indexes
    for (int i = 0; i < len; i++) {
        System.out.println(str.charAt(i) + "  has  index   " + i);
    }/*w ww  .  jav a 2 s  .c  om*/

}

From source file:MainClass.java

public static void main(String[] args) {
    try {//w w w. j a  va  2  s.c  o  m
        FileWriter fw = new FileWriter("LPT1:");

        PrintWriter pw = new PrintWriter(fw);
        String s = "www.java2s.com";

        int i, len = s.length();

        for (i = 0; len > 80; i += 80) {
            pw.print(s.substring(i, i + 80));
            pw.print("\r\n");
            len -= 80;
        }

        if (len > 0) {
            pw.print(s.substring(i));
            pw.print("\r\n");
        }

        pw.close();
    } catch (IOException e) {
        System.out.println(e);
    }
}

From source file:Main.java

public static void main(String args[]) {
    JTextArea area = new JTextArea(5, 20);
    area.setText("this is a test.");
    String charsToHighlight = "aeiouAEIOU";
    Highlighter h = area.getHighlighter();
    h.removeAllHighlights();/*www. ja  va2 s  . c  o m*/
    String text = area.getText().toUpperCase();
    for (int i = 0; i < text.length(); i += 1) {
        char ch = text.charAt(i);
        if (charsToHighlight.indexOf(ch) >= 0)
            try {
                h.addHighlight(i, i + 1, DefaultHighlighter.DefaultPainter);
            } catch (Exception ble) {
            }
    }
}

From source file:RegionMatchesDemo.java

public static void main(String[] args) {

    String searchMe = "Green Eggs and Ham";
    String findMe = "Eggs";
    int len = findMe.length();
    boolean foundIt = false;

    int i = 0;//from   ww  w.  ja  v a2  s  .co m
    while (!searchMe.regionMatches(i, findMe, 0, len)) {
        i++;
        foundIt = true;
    }
    if (foundIt) {
        System.out.println(searchMe.substring(i, i + len));
    }
}

From source file:Main.java

public static void main(String[] args) {
    String query = "name==p==?header=hello?aname=?????lname=lastname";
    String[] params = query.split("\\?");
    Map<String, String> map = new HashMap<String, String>();
    for (String param : params) {
        String name = param.split("=")[0];
        String value = param.substring(name.length(), param.length());
        map.put(name, value);/*from w  w  w. j ava  2  s . c  o  m*/
        System.out.println(name);
        if (name.equals("")) {
            value += "?";
        }
        System.out.println(value.replaceAll(" ", ""));
    }
}

From source file:AddingActionCommandActionListenerSample.java

public static void main(String args[]) {
    final JFrame frame = new JFrame("Default Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextField textField = new JTextField();
    frame.add(textField, 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(frame, "Can't leave.", "Error Dialog", JOptionPane.ERROR_MESSAGE);
                return false;
            } else {
                return true;
            }//from   w  w  w  .jav  a2s  .com
        }
    };
    textField.setInputVerifier(verifier);

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

From source file:ReadConsole.java

public static void main(String args[]) throws Exception {
    InputStreamReader isr = new InputStreamReader(System.in);

    BufferedReader br = new BufferedReader(isr);

    String s;
    while ((s = br.readLine()) != null) {
        System.out.println(s.length());
    }/*from  ww  w  .  j  a  v a  2 s  .c  om*/
    isr.close();
}

From source file:Stack.java

public static void main(String[] args) {
    String input = "input";
    int stackSize = input.length();

    Stack theStack = new Stack(stackSize);

    for (int j = 0; j < input.length(); j++) {
        char ch = input.charAt(j);
        theStack.push(ch);/*from w  w  w .j  a  va2s  . c om*/
    }

    while (!theStack.isEmpty()) {
        char ch = theStack.pop();
        System.out.println(ch);
    }
}

From source file:CountVowels.java

public static void main(String[] args) {
    System.out.print("Enter a string: ");
    String s = sc.nextLine();

    int vowelCount = 0;

    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if ((c == 'A') || (c == 'a') || (c == 'E') || (c == 'e') || (c == 'I') || (c == 'i') || (c == 'O')
                || (c == 'o') || (c == 'U') || (c == 'u'))
            vowelCount++;/*from  w ww. j a  va2s.  co  m*/
    }
    System.out.println("That string contains " + vowelCount + " vowels.");
}