首页 > 解决方案 > 使用子字符串来保留字符串的上一个和下一个索引,而不在 Java 中使用数组

问题描述

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

String myString = "1|name|lastname|email|tel" + "\n" + "2|name|lastname|email|tel" + "\n" + ...etc 在我的作业中我不能使用数组或除 String 和 System 之外的其他类。

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

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

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

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

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

如果用户输入 2 作为 id,我需要子串上一个索引 "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;
        }

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

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

谢谢

标签: javasubstring

解决方案


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

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");
    }
}

推荐阅读