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) {
    String n = "level";
    boolean right = true;
    int f = n.length() - 1;
    for (int i = 0; i < n.length(); i++) {
        if (n.charAt(i) != n.charAt(f - i)) {
            right = false;/*from  www . ja  v a2  s.  com*/
        }
    }
    System.out.println("The word is " + right);
}

From source file:Main.java

public static void main(String[] args) {
    String string = "java 2s.com java";
    int length = string.length();
    if (length < 2) {
        System.out.println(string);
        return;//from w w w .ja  v a 2  s .c o  m
    }

    System.out.print(string.charAt(0));
    for (int i = 1; i < length; i++) {
        if (string.charAt(i) != string.charAt(i - 1)) {
            System.out.print(string.charAt(i));
        }
    }
}

From source file:example.ToCommonsEmpty.java

public static void main(String[] args) {
    String s = "";

    boolean a = s == null || s.length() == 0;
    boolean b = s == null || s.isEmpty();
    boolean c = s != null && s.isEmpty();
    boolean d = s != null && s.length() == 0;
}

From source file:MainClass.java

License:asdf

public static void main(String[] argv) throws Exception {
    CharBuffer buffer = CharBuffer.allocate(100);

    String string = "asdf";

    for (int i = 0; i < string.length(); i++) {
        buffer.put(string.charAt(i));//from  w  w w . j a v a  2s  .c  o  m
    }

    buffer.flip();
    drainBuffer(buffer);
    buffer.clear();
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    String s = "This is a test.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));//from  www . jav  a2 s  .co m
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.size());
    ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
    int inBytes = inStream.available();
    System.out.println("inStream has " + inBytes + " available bytes");
    byte inBuf[] = new byte[inBytes];
    int bytesRead = inStream.read(inBuf, 0, inBytes);
    System.out.println(bytesRead + " bytes were read");
    System.out.println("They are: " + new String(inBuf));
}

From source file:ByteArrayIOApp.java

public static void main(String args[]) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    String s = "This is a test.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));/* w w w  . j  av a 2  s.com*/
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.size());
    ByteArrayInputStream inStream;
    inStream = new ByteArrayInputStream(outStream.toByteArray());
    int inBytes = inStream.available();
    System.out.println("inStream has " + inBytes + " available bytes");
    byte inBuf[] = new byte[inBytes];
    int bytesRead = inStream.read(inBuf, 0, inBytes);
    System.out.println(bytesRead + " bytes were read");
    System.out.println("They are: " + new String(inBuf));
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    StringWriter outStream = new StringWriter();
    String s = "This is a test from j a va2s.com.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));//from  w  w  w  . j  av  a 2 s  .c o  m
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.toString().length());
}

From source file:Main.java

public static void main(String[] args) {
    String str = "12345";
    int counter = 0;
    for (int i = 0; i < str.length(); i++) {
        if (Character.isLetter(str.charAt(i)))
            counter++;/*  w ww.j  ava  2 s.com*/
    }
    System.out.println(counter + " letters.");
}

From source file:StringIOApp.java

public static void main(String args[]) throws IOException {
    StringWriter outStream = new StringWriter();
    String s = "This is a test.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));//  w ww .  j a v a2s . co m
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.toString().length());
    StringReader inStream;
    inStream = new StringReader(outStream.toString());
    int ch = 0;
    StringBuffer sb = new StringBuffer("");
    while ((ch = inStream.read()) != -1)
        sb.append((char) ch);
    s = sb.toString();
    System.out.println(s.length() + " characters were read");
    System.out.println("They are: " + s);
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    CharArrayWriter outStream = new CharArrayWriter();
    String s = "This is a test.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));//from w  w w.j  a va 2 s.  c o m
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.size());
    CharArrayReader inStream;
    inStream = new CharArrayReader(outStream.toCharArray());
    int ch = 0;
    StringBuffer sb = new StringBuffer("");
    while ((ch = inStream.read()) != -1)
        sb.append((char) ch);
    s = sb.toString();
    System.out.println(s.length() + " characters were read");
    System.out.println("They are: " + s);
}