Character array convert and find : Character « Data Type « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Data Type » CharacterScreenshots 
Character array convert and find
  
// Copyright (c) 2003-2009, Jodd Team (jodd.org). All Rights Reserved.


/**
 * Various character and character sequence utilities.
 */
 class CharUtil {

  // ---------------------------------------------------------------- to byte array


  /**
   * Converts char array into byte array by stripping high byte.
   */
  public static byte[] toByteArray(char[] carr) {
    if (carr == null) {
      return null;
    }
    byte[] barr = new byte[carr.length];
    for (int i = 0; i < carr.length; i++) {
      barr[i(bytecarr[i];
    }
    return barr;
  }

  /**
   * Converts char array to byte array using provided encoding.  
   */
  public static byte[] toByteArray(char[] carr, String charsetthrows Exception{
    return new String(carr).getBytes(charset);
  }

  /**
   * Converts char array into ASCII array.
   @see #toAscii(char) 
   */
  public static byte[] toAsciiArray(char[] carr) {
    if (carr == null) {
      return null;
    }
    byte[] barr = new byte[carr.length];
    for (int i = 0; i < carr.length; i++) {
      barr[i(bytetoAscii(carr[i]);
    }
    return barr;
  }


  /**
   * Converts char sequence into byte array. Chars are truncated to byte size.
   */
  public static byte[] toByteArray(CharSequence charSequence) {
    if (charSequence == null) {
      return null;
    }
    byte[] barr = new byte[charSequence.length()];
    for (int i = 0; i < barr.length; i++) {
      barr[i(bytecharSequence.charAt(i);
    }
    return barr;
  }

  /**
   * Converts char sequence into ASCII array.
   */
  public static byte[] toAsciiArray(CharSequence charSequence) {
    if (charSequence == null) {
      return null;
    }
    byte[] barr = new byte[charSequence.length()];
    for (int i = 0; i < barr.length; i++) {
      barr[i(bytetoAscii(charSequence.charAt(i));
    }
    return barr;
  }

  // ---------------------------------------------------------------- to char array

  /**
   * Converts byte array to char array by simply extending.
   */
  public static char[] toCharArray(byte[] barr) {
    if (barr == null) {
      return null;
    }
    char[] carr = new char[barr.length];
    for (int i = 0; i < barr.length; i++) {
      carr[i(charbarr[i];
    }
    return carr;
  }

  /**
   * Converts byte array of specific encoding to char array.
   */
  public static char[] toCharArray(byte[] barr, String charsetthrows Exception {
    return new String(barr, charset).toCharArray();
  }


  /**
   * Returns ASCII value of a char. In case of overload, 0x3F is returned.
   */
  public static int toAscii(char c) {
    if (c <= 0xFF) {
      return c;
    else {
      return 0x3F;
    }
  }

  // ---------------------------------------------------------------- find


  /**
   * Match if one character equals to any of the given character.
   *
   @return <code>true</code> if characters match any character from given array,
   *         otherwise <code>false</code>
   */
  public static boolean equalsOne(char c, char[] match) {
    for (char aMatch : match) {
      if (c == aMatch) {
        return true;
      }
    }
    return false;
  }

  /**
   * Finds index of the first character in given array the matches any from the
   * given set of characters.
   *
   @return index of matched character or -1
   */
  public static int findFirstEqual(char[] source, int index, char[] match) {
    for (int i = index; i < source.length; i++) {
      if (equalsOne(source[i], match== true) {
        return i;
      }
    }
    return -1;
  }

  /**
   * Finds index of the first character in given array the matches any from the
   * given set of characters.
   *
   @return index of matched character or -1
   */
  public static int findFirstEqual(char[] source, int index, char match) {
    for (int i = index; i < source.length; i++) {
      if (source[i== match) {
        return i;
      }
    }
    return -1;
  }


  /**
   * Finds index of the first character in given array the differs from the
   * given set of characters.
   *
   @return index of matched character or -1
   */
  public static int findFirstDiff(char[] source, int index, char[] match) {
    for (int i = index; i < source.length; i++) {
      if (equalsOne(source[i], match== false) {
        return i;
      }
    }
    return -1;
  }

  /**
   * Finds index of the first character in given array the differs from the
   * given set of characters.
   *
   @return index of matched character or -1
   */
  public static int findFirstDiff(char[] source, int index, char match) {
    for (int i = index; i < source.length; i++) {
      if (source[i!= match) {
        return i;
      }
    }
    return -1;
  }

  // ---------------------------------------------------------------- is char at

  public static boolean isCharAtEqual(char[] source, int index, char match) {
    if ((index < 0|| (index >= source.length)) {
      return false;
    }
    return source[index== match;
  }

  public static boolean isCharAtEqual(CharSequence source, int index, char match) {
    if ((index < 0|| (index >= source.length())) {
      return false;
    }
    return source.charAt(index== match;
  }

  // ---------------------------------------------------------------- is

  /**
   * Returns <code>true</code> if character is a white space.
   * White space definition is taken from String class (see: <code>trim()</code>
   */
  public static boolean isWhitespace(char c) {
    return c <= ' ';
  }

  /**
   * Returns <code>true</code> if specified character is lowercase ASCII.
   * If user uses only ASCIIs, it is much much faster.
   */
  public static boolean isLowercaseLetter(char c) {
    return (c >= 'a'&& (c <= 'z');
  }

  /**
   * Returns <code>true</code> if specified character is uppercase ASCII.
   * If user uses only ASCIIs, it is much much faster.
   */
  public static boolean isUppercaseLetter(char c) {
    return (c >= 'A'&& (c <= 'Z');
  }

  public static boolean isLetter(char c) {
    return ((c >= 'a'&& (c <= 'z')) || ((c >= 'A'&& (c <= 'Z'));
  }

  public static boolean isDigit(char c) {
    return (c >= '0'&& (c <= '9');
  }

  public static boolean isLetterOrDigit(char c) {
    return isDigit(c|| isLetter(c);
  }

  public static boolean isWordChar(char c) {
    return isDigit(c|| isLetter(c|| (c == '_');
  }

  public static boolean isPropertyNameChar(char c) {
    return isDigit(c|| isLetter(c|| (c == '_'|| (c == '.'|| (c == '['|| (c == ']');
  }

  // ---------------------------------------------------------------- conversions

  /**
   * Uppers lowercase ASCII char.
   */
  public static char toUpperAscii(char c) {
    if (isLowercaseLetter(c)) {
      c -= (char0x20;
    }
    return c;
  }


  /**
   * Lowers uppercase ASCII char.
   */
  public static char toLowerAscii(char c) {
    if (isUppercaseLetter(c)) {
      c += (char0x20;
    }
    return c;
  }

}

   
    
  
Related examples in the same category
1. Java char: char is 16 bit type and used to represent Unicode characters. Range of char is 0 to 65,536.
2. Character class creates primitives that wrap themselves around data items of the char data type
3. Create Character objects and compare them
4. Use Character.isLowerCase, Character.isUpperCase to check the letter case
5. Use Character.isLetter to check if a char is a letter
6. Count letters in a String
7. Use Character.isDigit to check the if a char is a digit
8. Validate if a String contains only numbers
9. Check if a character representing a number
10. Check if a character representing an alphabet
11. If a character is uppercase
12. if a character is lowercase
13. Is character a digit, letter, white space, lower case or upper case character
14. switch with char value
15. Escape Sequence Characters
16. Char is IntChar is Int
17. Convert from integer to ASCII code (byte)
18. Plus one to char variable
19. Store unicode in a char variable
20. All static information about a character
21. Compare Two Java char Arrays
22. Convert from ASCII code to String
23. Copy char array to string
24. Convert to hex
25. Determining a Character's Unicode Block
26. Determining If a String Is a Legal Java Identifier
27. Count the number of bytes included in the given char[].
28. Count the number of chars included in the given byte[].
29. StringBuffer based on char array
30. String based on char array
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.