简体   繁体   English

在Java 8中从Pojo中删除不可打印的字符

[英]Remove non printable character from pojo in java 8

I want to remove the non-printable character only for the String fields in the poject, I know we can use 我只想删除项目中String字段的不可打印字符,我知道我们可以使用

public String removeNonPrintable(String field) {
    return field.replaceAll("[^A-Za-z0-9]", "");
}

to remove the non-printable characters from string. 从字符串中删除不可打印的字符。

But I want to have the generic method like: 但我想拥有通用方法,例如:

public <T> T removeNonPrintable(Class<T> myClassObject) {
    /// Get only the string and remove non-printable code stuffs
    return removedNonPrintableCharactersmyClassObject;
}

Can anyone please help me to do it? 有人可以帮我做吗?

This question may be duplicate, but I failed to found the exact solutions for it. 这个问题可能是重复的,但是我没有找到确切的解决方案。

You could use reflection, which would be problematic and not guaranteed to work for fields that have deep structures. 您可以使用反射,这将是有问题的,并且不能保证适用于结构较深的字段。

Or you could just run the instance through your code: 或者,您可以只通过代码运行实例

public String removeNonPrintable(Object obj) {
    return String.valueOf(obj).replaceAll("[^A-Za-z0-9]", "");
}

Note: String.valueOf() is used to avoid having to deal with obj being null . 注意: String.valueOf()用于避免必须处理objnull

You might have more success if you changed the transformation to: 如果将转换更改为以下内容,则可能会获得更大的成功:

replaceAll("[^ -~]", "")

which will remove everything not a "regular" ascii char (ie between decimal 32 and 126 inclusive). 这将删除所有 “常规” ASCII字符(即,十进制的32至126之间)。

Following is working: 以下工作:

public static void removeNonPrintable(Node node) throws NoSuchFieldException, IllegalAccessException{
        Field[] fields = Node.class.getDeclaredFields();
        for(int i=0;i<fields.length;i++){
            if(fields[i].getType().equals(String.class)) {
                fields[i].set(node, removeNonPrintable(fields[i].get(node).toString()));
                System.out.println(fields[i].get(node));
            }
        }
    }

class Node{
     String left;
     String right;
     int data;
    public Node(String left, String right, Integer data){
        this.left = left;
        this.right = right;
        this.data = data;
    }
}

It will work for a particular class(eg Node). 它适用于特定的类(例如Node)。 I am trying to make it work with Class<T> but it's giving some exception right now. 我正在尝试使其与Class<T>但是现在正在提供一些例外。

You can do it using reflection , but it is not recommended:- 您可以使用反射来做到这一点,但不建议这样做:

public static void main(String[] args) throws IllegalAccessException {
    Person person = new Person();
    person.setName("John***");
    person.setAge(12);

    final Field[] fields = person.getClass().getDeclaredFields();
    for (Field field : fields) {
        if (field.getType() == String.class) {
            field.set(person, removeNonPrintable((String) field.get(person)));
        }
    }
    System.out.println(person.getName());
    System.out.println(person.getAge());
}

//using the regex provided by OP
private static String removeNonPrintable(String s) {
    return s.replaceAll("[^A-Za-z0-9]", "");
}

And you'll have to change String fields to public accessor. 而且,您必须将String字段更改为public访问器。 This prints:- 打印:-

John
12

A better approach is to do this in getters or setters of String fields. 更好的方法是在String字段的getter或setter方法中执行此操作。

Example:- 例:-

public String getName() {
    return removeNonPrintable(name);
}

OR 要么

public void setName(String name) {
    this.name = removeNonPrintable(name);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM