建立不区分大小写的电影数据库
Building case insensitive movie database
我正在做一项大学作业,我必须构建 4 个 类,其中一个是接口。
我需要确保字符串不区分大小写,例如"the godfather"、"The godfather"、"The Godfather" 被视为同一部电影。我该怎么做?
要使字符串不区分大小写,您应该使用 toLowerCase() 方法并将您操作的所有字符串设为小写形式。你可以这样做:
stringA.toLowerCase();
希望这对您有所帮助,如果这不是您要找的,请随时告诉我! :D
除了 Aaron 所说的,我想指出这实际上取决于您的实施。顺便说一句,您的任务是创建一个 "search" 函数来搜索数据库。正确存储电影名称将以实际格式 ("The Godfather") 保存它们,但是当启动搜索时,您希望使用 String .equalsIgnoreCase() 函数将搜索查询与数据库匹配。例如:
String a = "The Godfather"
String b = "the godfather"
String c = "The Godfather"
String d = "thE GoDfaTher"
System.out.println(a.equalsIgnoreCase(b)); // Outputs true
System.out.println(a.equalsIgnoreCase(c)); // Outputs true
System.out.println(a.equalsIgnoreCase(d)); // Outputs true
System.out.println(a.equals(b)); // Outputs false
我正在做一项大学作业,我必须构建 4 个 类,其中一个是接口。
我需要确保字符串不区分大小写,例如"the godfather"、"The godfather"、"The Godfather" 被视为同一部电影。我该怎么做?
要使字符串不区分大小写,您应该使用 toLowerCase() 方法并将您操作的所有字符串设为小写形式。你可以这样做:
stringA.toLowerCase();
希望这对您有所帮助,如果这不是您要找的,请随时告诉我! :D
除了 Aaron 所说的,我想指出这实际上取决于您的实施。顺便说一句,您的任务是创建一个 "search" 函数来搜索数据库。正确存储电影名称将以实际格式 ("The Godfather") 保存它们,但是当启动搜索时,您希望使用 String .equalsIgnoreCase() 函数将搜索查询与数据库匹配。例如:
String a = "The Godfather"
String b = "the godfather"
String c = "The Godfather"
String d = "thE GoDfaTher"
System.out.println(a.equalsIgnoreCase(b)); // Outputs true
System.out.println(a.equalsIgnoreCase(c)); // Outputs true
System.out.println(a.equalsIgnoreCase(d)); // Outputs true
System.out.println(a.equals(b)); // Outputs false