在 Java 中使用子字符串保留字符串的上一个和下一个索引而不使用数组

Use substring to keep previous and next index of a string without using array in Java

我有一个看起来像这样的字符串(所有子字符串都由 return \n:

分隔

String myString = "1|姓名|姓氏|电子邮件|电话" + "\n" + "2|姓名|姓氏|电子邮件|电话" + "\n" + ...ETC 在我的作业中,除了 String 和 System.

,我不能使用数组或其他 类

我需要使用子字符串方法从该字符串中删除一个子字符串。

我使用扫描仪从用户那里获取 id,然后我必须将该 id 与我的字符串中的 id 进行比较,该 id 可以从 1 到 999。然后我必须保留所有子字符串,除了具有与用户输入相同的 id。 为此,我需要使用 substring 方法对前一个索引中的所有字符和下一个索引中的所有字符进行子字符串化。

我想保留所有字符,除了位于等于用户输入的 ID 和“\n”之间的字符之外的字符 是当前索引的结束字符。

这意味着如果我的字符串是这样组成的:

String myString = "1|name|lastname|email|tel" + "\n" +
                  "2|name|lastname|email|tel" + "\n" + 
                  "3|name|lastname|email|tel" + "\n";

如果用户输入2作为id,我需要把前面的index "1|name|lastname|email|tel" + "\n" 和后面的 "3|name|lastname|email|tel" + "\n";并将新字符串分配给一个变量。

我有一种方法可以从以下字符串中检索 ID:

public static String getIdContact (String contact) {
        int i = 0;
        String id = "";
        while(contact.charAt(i) != '|') {
            Id+= contact.charAt(i);
            i++;
        }
       return id;
}

我用另一种方法来达到下一个索引:

public static int nextIndex(int index, String carnet) {
   return carnet.indexOf("\n", index)+1;
}

和另一种从字符串中提取子字符串的方法:

public static String extractSubstring(String myString, char start, char end) {
        int indexStart;
        int indexEnd;
        String subString = null;
        if (myString != null) {
            indexStart = myString.indexOf(start);
            if (indexStart != -1) {
                indexEnd = myString.indexOf(end, indexStart + 1);
                if (indexEnd != -1) {
                    subString = myString.substring(indexStart + 1, indexEnd);
                }
            }
        }
        return subString;
    }

最后,我有了从字符串中删除子字符串的主要方法,我在该方法中调用了将用户给定的 ID 与字符串中的 ID 进行比较的方法。

public static String deleteContactFromMyString (String idContact, String myString) {
        String myNewString = "";
        String id = getIdContact(myString);

        *Here I have to compare the id given in parameter (id entered by user) to the id 
         returned from the getIdContact method (id in string)*

        *If both ids are equals, I need to substring all characters before that id and all characters after the "\n" and return the new string in the variable called myNewString*
         
        
         return myNewString;
        }

我很困惑,我不知道如何继续执行该子字符串。通常我会使用数组来做到这一点,但在我的作业中不允许使用它们。

如果有人能帮助我,我将不胜感激。

谢谢

我认为您使任务变得过于复杂。通过使用 String.indexOf 方法搜索 id+"|" 找到“to deleting”子字符串的初始索引,并调用它,例如 start。如果这是肯定的,则在找到的 ID 之后找到下一行分隔符 ("\n"),并确定“to deleting”子字符串的结束索引,并将其称为 end。通过将来自 0 to startend 的原始字符串的子字符串连接到原始字符串的末尾来确定最终结果。

public static void main(String[] args) throws IOException {
    String myString = "1|name|lastname|email|tel" + "\n" +
                      "2|name|lastname|email|tel" + "\n" + 
                      "3|name|lastname|email|tel" + "\n";
    int id = 2;
    int start = myString.indexOf(id+"|");
    if(start >= 0){
       int end = myString.indexOf("\n", start) + 1;
       String modified = myString.substring(0, start) + myString.substring(end);
       System.out.println(modified);
    }
    else{
       System.out.println("id not found");
    }
}