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

public static void main(String args[]) throws IOException {
    FileOutputStream outStream = new FileOutputStream("test.txt");
    String s = "This is a test.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));//from ww  w .  java2s .  c om
    outStream.close();
    FileInputStream inStream = new FileInputStream("test.txt");
    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));
    inStream.close();
    File f = new File("test.txt");
    f.delete();
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    StringWriter outStream = new StringWriter(100);
    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  ww  . j  av a2  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 palindrome = "Dot saw I was Tod";
    int len = palindrome.length();
    char[] tempCharArray = new char[len];
    char[] charArray = new char[len];

    // put original string in an array of chars
    for (int i = 0; i < len; i++) {
        tempCharArray[i] = palindrome.charAt(i);
    }/*from w w  w  .  j a  va  2s  .c  o m*/

    // reverse array of chars
    for (int j = 0; j < len; j++) {
        charArray[j] = tempCharArray[len - 1 - j];
    }

    String reversePalindrome = new String(charArray);
    System.out.println(reversePalindrome);
}

From source file:PrintfExamples.java

License:asdf

public static void main(String[] args) {

    String[] words = new String[] { "a", "ape", "asdfasdfasdfasdfasdfasdfasdfadsfasdf" };
    System.out.printf("%-10s %s\n", "Word", "Length");
    for (String word : words)
        System.out.printf("%-10.10s %s\n", word, word.length());

}

From source file:Main.java

public static void main(String[] args) {
    Map<Character, Integer> map = new TreeMap<>();

    String blah = "aaaabbbbddd";

    for (int i = 0; i < blah.length(); i++) {
        char c = blah.charAt(i);
        if (!map.containsKey(c)) {
            map.put(c, 1);/* ww w .  ja  va 2s  . c  o  m*/
        } else {
            map.put(c, (map.get(c) + 1));
        }
    }

    for (Map.Entry<Character, Integer> entry : map.entrySet()) {
        System.out.print(entry.getKey() + "" + entry.getValue());
    }
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    String source = "test";
    char buffer[] = new char[source.length()];
    source.getChars(0, source.length(), buffer, 0);

    FileWriter f0 = new FileWriter("file1.txt");
    for (int i = 0; i < buffer.length; i += 2) {
        f0.write(buffer[i]);/*from  w w  w  . ja  va  2 s  .  c o m*/
    }
    f0.close();

    FileWriter f1 = new FileWriter("file2.txt");
    f1.write(buffer);
    f1.close();

    FileWriter f2 = new FileWriter("file3.txt");

    f2.write(buffer, buffer.length - buffer.length / 4, buffer.length / 4);
    f2.close();
}

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  w  w  w .j ava 2 s .co  m
    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:MainClass.java

public static void main(String args[]) throws Exception {

    String[] words = new String[] { "a", "aaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" };
    System.out.printf("%-10s %s\n", "Word", "Length");
    for (String word : words)
        System.out.printf("%-10.10s %s\n", word, word.length());

}

From source file:Main.java

public static void main(String args[]) throws IOException {
    String tmp = "abcdefghijklmnopqrstuvwxyz";
    int length = tmp.length();
    char c[] = new char[length];

    tmp.getChars(0, length, c, 0);//from  w ww.j a  v a 2  s  . com
    CharArrayReader input1 = new CharArrayReader(c);
    CharArrayReader input2 = new CharArrayReader(c, 0, 5);

    int i;
    while ((i = input1.read()) != -1) {
        System.out.print((char) i);
    }

    while ((i = input2.read()) != -1) {
        System.out.print((char) i);
    }
}

From source file:AnagramApp.java

public static void main(String[] args) throws IOException {
    String input = "java2s.com";
    size = input.length();
    count = 0;//from   w w  w  . ja va 2  s .c  om
    charArray = new char[size];
    for (int j = 0; j < size; j++)
        charArray[j] = input.charAt(j);
    doAnagram(size);
}