简体   繁体   English

为什么这个java方法不起作用? 它应该消除字符串中的空白

[英]why doesn't this java method work? it should eliminate empty spaces in Strings

here's the method : 这是方法:

public static String normalizza(String x) {

    for (int i = x.length(); i > 0; i--) {
        if (x.substring(i, x.length()).equalsIgnoreCase(" ")) {
            x = x.substring(0, i);
        }
    }

    return x;
}

i should read a String from a random access file and eliminate empty spaces to find the object position 我应该从随机访问文件中读取字符串,并消除空白以查找对象位置

these are the attributes of the class : 这些是该类的属性:

public class Iscritto {
private int id;
private String nome;
private String cognome;
private String dataNascita;

this is the search method for the attribute "nome" ( the method "normalizza" works with this one ) : 这是属性“ nome”的搜索方法(方法“ normalizza”与此合用):

private static void cercaNome() {
    Iscritto a = new Iscritto();
    try {
        File file = new File("C:\\temp\\iscritti.dat");
        RandomAccessFile ra = new RandomAccessFile(file, "r");
        String nome = JOptionPane.showInputDialog("Inserisci nome da cercare:");
        ra.seek(0);
        a.leggi(ra);
        String nomeControllato = a.getNome();
        nomeControllato = normalizza(nomeControllato);
        int conta = 0;

        if (nomeControllato.equalsIgnoreCase(nome)) {
            int b = (int) ra.getFilePointer() - 2;
            int position = b / 153;
            ra.seek(position * 153);
            a.leggi(ra);
            System.out.println("iscritto:   " + a);
        } else {
            while (!nomeControllato.equalsIgnoreCase(nome)) {
                conta++;
                ra.seek(conta * 153);
                a.leggi(ra);
                nomeControllato = a.getNome();
                nomeControllato = normalizza(nomeControllato);

                if (nomeControllato.equalsIgnoreCase(nome)) {
                    int b = (int) ra.getFilePointer() - 2;
                    int position = b / 153;
                    ra.seek(position * 153);
                    a.leggi(ra);
                    System.out.println("iscritto:   " + a);
                }

            }
        }
        ra.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

this is the search method for the attribute "data" ( normalizza doesn't work for this one ) : 这是属性“数据”的搜索方法(normalizza不适用于此方法):

private static void cercaData() {
    Iscritto a = new Iscritto();
    try {
        File file = new File("C:\\temp\\iscritti.dat");
        RandomAccessFile ra = new RandomAccessFile(file, "r");
        String data = JOptionPane.showInputDialog("Inserisci data da cercare (example : 12-MAG-2018):");
        ra.seek(0);
        a.leggi(ra);
        String dataControllata = a.getDataNascita();
        dataControllata = normalizza(dataControllata);
        int conta = 0;

        if (dataControllata.equalsIgnoreCase(data)) {
            int b = (int) ra.getFilePointer() - 2;
            int position = b / 153;
            ra.seek(position * 153);
            a.leggi(ra);
            System.out.println("iscritto:   " + a);
        } else {
            while (!dataControllata.equalsIgnoreCase(data)) {
                conta++;
                ra.seek(conta * 153);
                a.leggi(ra);
                dataControllata = a.getDataNascita();
                dataControllata = normalizza(dataControllata);

                if (dataControllata.equalsIgnoreCase(data)) {
                    int b = (int) ra.getFilePointer() - 2;
                    int position = b / 153;
                    ra.seek(position * 153);
                    a.leggi(ra);
                    System.out.println("iscritto:   " + a);
                }

            }
        }
        ra.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

if i search a "data" and it's not the last of the file it will give me an infinite "null" output 如果我搜索“数据”,而不是文件的最后一个,它将给我无限的“空”输出

If you want to eliminate empty spaces at the end of your String, simply use the trim() method. 如果要消除字符串末尾的空白,只需使用trim()方法。

System.out.println(" my String  ".trim()); // prints 'my String'

If you need to eliminate all whitespaces in your string, then use replace(String, String) 如果需要消除字符串中的所有空格,请使用replace(String, String)

System.out.println(" my String  ".replace(" ", "")); // prints 'myString'

Assuming you have a reason not to use String.trim(), you could do a much simpler implementation: 假设您有理由不使用String.trim(),则可以执行更简单的实现:

start i from the end of string, and look backward at each .charAt(i) until != ' '. 从字符串末尾开始i,然后向后看每个.charAt(i),直到!=''。 Then substring just once, cutting after that non-space char. 然后,仅将子字符串一次,切掉该非空格字符。

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

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