Convert String to Alpha Number - Java java.lang

Java examples for java.lang:String Parse

Description

Convert String to Alpha Number

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String source = "java2s.com";
        System.out.println(toAlphaNum(source));
    }//ww  w . j  ava 2 s  .  c o m

    public static String toAlphaNum(String source) {
        byte[] sourceb = source.getBytes();
        for (int i = 0; i < sourceb.length; i++) {
            if (sourceb[i] < '0' || (sourceb[i] > '9' && sourceb[i] < 'A')
                    || (sourceb[i] > 'Z' && sourceb[i] < '_')
                    || sourceb[i] > 'z')
                sourceb[i] = '_';
        }
        return new String(sourceb);

    }
}

Related Tutorials