使用 compareToIgnoreCase() 将条目与字符串中的子字符串进行比较,而不使用数组 [英] Using compareToIgnoreCase() to compare an entry to substring in string without using array

查看:26
本文介绍了使用 compareToIgnoreCase() 将条目与字符串中的子字符串进行比较,而不使用数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的作业中,禁止使用集合和任何数组.我们可以使用 String Tokenizer,但不允许使用除 String 和 System 之外的任何其他类.此解决方案必须适用于任意数量的条目.

In my assignment it's forbidden to use collections and any arrays. We are allowed to use String Tokenizer but any other classes than String and System are not allowed. This solution has to work with any number of entries.

我有一个看起来像这样的字符串:

I have a string which look like this :

1|Aaron|Peter|3063543030|john@gmail.com + "\n" 
2|Buffet|Anthony|3063543030|john@gmail.com + "\n" 
3|Dunty|Richard|3063543030|john@gmail.com 

例如,如果条目是 4|Doe|John|3063543030|john@gmail.com,那么将使用 compareToIgnoreCase() 进行比较,并且条目将被插入到 3|Dunty|Richard|3063543030|john 之前@gmail.com

For example, if the entry is 4|Doe|John|3063543030|john@gmail.com then the comparison will be make using compareToIgnoreCase() and the entry will be insert just before 3|Dunty|Richard|3063543030|john@gmail.com

这里我有一个获取条目名称的方法,它使用 String Tokenizer :

Here I have a method that get the entry name and it's using String Tokenizer :

 public static String obtenirNomContact (String contactLigne) {
        StringTokenizer tokenizer = new StringTokenizer(contactLigne, "|");
        String id = tokenizer.nextToken();
        String nom = tokenizer.nextToken();
        String prenom = tokenizer.nextToken();


        return nom;
    }

在这个方法中,我在字符串中插入条目并使用 compareToIgnoreCaseMethod() 进行比较

In this method I insert the entry in the string and make the comparison using compareToIgnoreCaseMethod()

 public static String insertEntryInString
        (String myString, String entry) {
            int result = 0;
            String entryName = "";
            String myString = "";
            String entry = "";
            String nameInString = "";
    
            if (myString != null) {
                myString += entry + "\n";
    
                do {
                    entryName = getContactName(entry);
                    nameInString = getContactName(myString);
    
                    result = entryName.compareToIgnoreCase(nameInString);
                    if (result < 0) {
    
                        entry += entryName + "\n";
                        entry += nameInString + "\n";
                    } else {
                        entry += nameInString  + "\n";
                        entry += entryName + "\n";
                    }
    
                } while (result > 0);
                myString += entry + "\n";
                System.out.println(myString);
            }
          
            return myString;  
        }

我现在尝试做的没有任何成功是仅当比较结果等于 1 或 0 时才在字符串中插入条目.

What I'm trying to do without any success for now is to insert the entry in string only if the result of the comparison is equal to 1 or 0.

如果有人能帮我解决这个问题,我将不胜感激.

I would appreciate if someone could help me resolve that problem.

谢谢

推荐答案

假设实现了以下帮助方法来获取联系人的姓氏和姓名:

Let's assume the following helper methods are implemented to get surname and name of the contact:

static String getSurname(String contact) {
    StringTokenizer st = new StringTokenizer(contact, "|");
    st.nextToken(); // skip id
    return st.nextToken();
}

static String getName(String contact) {
    StringTokenizer st = new StringTokenizer(contact, "|");
    st.nextToken(); // skip id
    st.nextToken(); // skip surname
    return st.nextToken();
}

然后将新联系人插入到已排序的列表"中的方法.用 '\n' 分隔的联系人可以改写如下:

Then the method to insert a new contact into sorted "list" of contacts separated with '\n' may be rewritten as follows:

private static final String NL = "\n";

static String insertContact(String contact, String data) {
    String newSurname = getSurname(contact);
    String newName = getName(contact);
    
    StringTokenizer st = new StringTokenizer(data, NL);
    StringBuilder sb = new StringBuilder();
    boolean inserted = false;
    
    while (st.hasMoreTokens()) {
        String curr = st.nextToken();
        String currSurname = getSurname(curr);
        String currName = getName(curr);
        
        if (!inserted && (currSurname.compareToIgnoreCase(newSurname) > 0 || (currSurname.compareToIgnoreCase(newSurname) == 0 && currName.compareToIgnoreCase(newName) > 0))) {
            inserted = true;
            System.out.println("Inserting before " + curr);

            sb.append(sb.length() > 0 ? NL : "").append(contact);
        }
        sb.append(sb.length() > 0 ? NL : "").append(curr);
    }
    if (!inserted) {
        sb.append(sb.length() > 0 ? NL : "").append(contact);
    }
    System.out.println("Data:\n" + sb);
    System.out.println("---------");
    return sb.toString();
}

测试:

String data = "1|Abercrombie|Peter|3063543030|john@gmail.com\n" 
            + "2|Buffet|Anthony|3063543030|john@gmail.com\n" 
            + "3|Dunty|Richard|3063543030|john@gmail.com";

data = insertContact("4|Doe|John|3063543030|john@gmail.com", data);
data = insertContact("5|Aaron|Paul|5551234567|apaul@breakingbad.com", data);
data = insertContact("6|Gilligan|Vince|5559123456|vinceg@breakingbad.com", data);

输出:

Inserting before 3|Dunty|Richard|3063543030|john@gmail.com
Data:
1|Abercrombie|Peter|3063543030|john@gmail.com
2|Buffet|Anthony|3063543030|john@gmail.com
4|Doe|John|3063543030|john@gmail.com
3|Dunty|Richard|3063543030|john@gmail.com
---------
Inserting before 1|Abercrombie|Peter|3063543030|john@gmail.com
Data:
5|Aaron|Paul|5551234567|apaul@breakingbad.com
1|Abercrombie|Peter|3063543030|john@gmail.com
2|Buffet|Anthony|3063543030|john@gmail.com
4|Doe|John|3063543030|john@gmail.com
3|Dunty|Richard|3063543030|john@gmail.com
---------
Data:
5|Aaron|Paul|5551234567|apaul@breakingbad.com
1|Abercrombie|Peter|3063543030|john@gmail.com
2|Buffet|Anthony|3063543030|john@gmail.com
4|Doe|John|3063543030|john@gmail.com
3|Dunty|Richard|3063543030|john@gmail.com
6|Gilligan|Vince|5559123456|vinceg@breakingbad.com
---------

这篇关于使用 compareToIgnoreCase() 将条目与字符串中的子字符串进行比较,而不使用数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆