List of usage examples for java.lang String length
public int length()
From source file:Main.java
public static void main(String[] args) { String[] strings = { "Here", "are", "some", "sample", "strings", "to", "be", "sorted" }; Arrays.sort(strings, new Comparator<String>() { public int compare(String s1, String s2) { int c = s2.length() - s1.length(); if (c == 0) c = s1.compareToIgnoreCase(s2); return c; }/*from w w w .j ava 2 s .c o m*/ }); for (String s : strings) System.out.print(s + " "); }
From source file:Main.java
public static void main(String args[]) { String name = "premkumarg"; int len = name.length(); char[] c = name.toCharArray(); for (int i = 0; i < len - 1; i = i + 2) { char temp = c[i]; c[i] = c[i + 1];/*from w w w . ja v a 2 s . c om*/ c[i + 1] = temp; } System.out.println("Swapping string is: "); System.out.println(c); }
From source file:MainClass.java
public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String s; while ((s = in.readLine()) != null && s.length() != 0) System.out.println(s);//ww w . j a v a 2 s . c o m // An empty line or Ctrl-Z terminates the program }
From source file:Main.java
public static void main(String[] args) { String numbers = "1234567890"; for (int i = 0; i < numbers.length(); i++) { if (Character.isDigit(numbers.charAt(i))) { System.out.println(numbers.charAt(i) + " is a number."); } else {/*from www .ja v a2s.co m*/ System.out.println(numbers.charAt(i) + " not a number."); } } }
From source file:Main.java
public static void main(String[] args) { boolean flag = true; String str = "123"; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) > 48 && str.charAt(i) < 58) { flag = false;/* w w w . j a v a2s. c om*/ break; } } if (flag == true) { System.out.println("String is a valid String.."); } else { System.out.println("String contains number"); } }
From source file:Main.java
public static void main(String args[]) { String s = "java2s.com"; int l;// w ww. j ava 2 s . com l = s.length(); for (int i = 0; i < l; i++) { int padding = s.length() - i; if (padding > 0) { System.out.printf("%" + padding + "s", " "); } for (int j = 0; j < i; j++) { System.out.printf("%c ", s.charAt(j)); } System.out.printf("%c\n", s.charAt(i)); } }
From source file:Main.java
public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String tmp = br.readLine(); int length = tmp.length(); char c[] = new char[length]; tmp.getChars(0, length, c, 0);/*from ww w .ja va 2 s . c o m*/ CharArrayReader input1 = new CharArrayReader(c); int i; System.out.print("input1 is:"); while ((i = input1.read()) != -1) { System.out.print((char) i); } }
From source file:Main.java
public static void main(String args[]) throws IOException { BufferedReader buff = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the char:"); String str = buff.readLine(); for (int i = 0; i < str.length(); ++i) { char c = str.charAt(i); int j = (int) c; System.out.println("ASCII OF " + c + " = " + j + "."); }// w ww . j a v a2 s. c om }
From source file:Main.java
public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("acowa"); list.add("cow"); list.add("aow"); Collections.sort(list, new Comparator<String>() { @Override/*from www. ja v a 2 s .c o m*/ public int compare(String o1, String o2) { if (o1.length() > o2.length()) { return 1; } else { return o1.compareTo(o2); } } }); System.out.println(list); }
From source file:Main.java
public static void main(String[] args) { int y = 0;/* ww w . j a v a 2 s .co m*/ char letter = 'e'; String food = "this is a test"; for (int x = 0; x < food.length(); x++) { if (food.charAt(x) == letter) { y++; } } System.out.println(y); }