1

My problem is that I want to check if my String matches ASCII character set.

I tried to use Guava library in my android project. The problem is that this library has too much weight (installed app size was 41 MB and with Guava library is become 45MB).

From Guava library I only need this:

CharMatcher.ascii().matchesAllOf();

Do you have any ideas how should i check my string correctly,or is there any other light-weight libraries?

Thanks!

Ololoking
  • 1,577
  • 4
  • 22
  • 37
  • 1
    Look at Guava source and copy that method and other call stack to your local. https://github.com/google/guava/blob/master/guava/src/com/google/common/base/CharMatcher.java – kosa Mar 02 '16 at 15:47
  • @Nambari according to your answer i will not have any problems with licence? – Ololoking Mar 02 '16 at 15:50
  • @Diyarbakir Please read the question before flagging. – Seth Mar 02 '16 at 15:50
  • It's open source, AFAIK no license issues (but I have no authority to confirm that because I am not a contributor to that project). – kosa Mar 02 '16 at 15:52
  • 2
    You should go on with Guava, but shrink your jar file! https://github.com/google/guava/wiki/UsingProGuardWithGuava – Olivier Grégoire Mar 02 '16 at 16:08
  • `text.matches("\\A\\p{ASCII}*\\z")` is [Arne](http://stackoverflow.com/a/3585284/2226988)'s answer in linked question. – Tom Blodget Mar 02 '16 at 17:41

3 Answers3

3

The java code is:

public static boolean isAsciiPrintable(String str) {
  if (str == null) {
      return false;
  }
  int sz = str.length();
  for (int i = 0; i < sz; i++) {
      if (isAsciiPrintable(str.charAt(i)) == false) {
          return false;
      }
  }
  return true;
  }
    public static boolean isAsciiPrintable(char ch) {
  return ch >= 32 && ch < 127;
  }
}

Ref: http://www.java2s.com/Code/Java/Data-Type/ChecksifthestringcontainsonlyASCIIprintablecharacters.htm

Aajan
  • 927
  • 1
  • 10
  • 23
-1

You can try this:

private static boolean isAllASCII(String input) {
    boolean isASCII = true;
    for (int i = 0; i < input.length(); i++) {
        int c = input.charAt(i);
        if (c > 0x7F) {
            isASCII = false;
            break;
        }
    }
    return isASCII;
}

ref In Java, is it possible to check if a String is only ASCII?

Community
  • 1
  • 1
GiapLee
  • 436
  • 2
  • 8
-2

From RealHowTo's answer to In Java, is it possible to check if a String is only ASCII?

You can do it with java.nio.charset.Charset.

import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;

public class StringUtils {

  static CharsetEncoder asciiEncoder = 
      Charset.forName("US-ASCII").newEncoder(); // or "ISO-8859-1" for ISO Latin 1

  public static boolean isPureAscii(String v) {
    return asciiEncoder.canEncode(v);
  }

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

     String test = "Réal";
     System.out.println(test + " isPureAscii() : " + StringUtils.isPureAscii(test));
     test = "Real";
     System.out.println(test + " isPureAscii() : " + StringUtils.isPureAscii(test));

     /*
      * output :
      *   Réal isPureAscii() : false
      *   Real isPureAscii() : true
      */
  }
}

Detect non-ASCII character in a String

Community
  • 1
  • 1
Chandan kushwaha
  • 941
  • 6
  • 26