简体   繁体   English

我如何分隔字符串,以便“ cat!”将变为“ cat!”

[英]How do i seperate a String so that “cat!” will be “cat !”

I believe this program will work for most instances except one. 我相信该程序将适用于除一种情况以外的大多数情况。 I added spaces in indexOF() so words like cathrine and dogsuit lammas Would all be accepted as non profane. 我在indexOF()中添加了空格,因此诸如cathrine和dogsuit lammas之类的词都将被视为非亵渎。 The only problem i see is if the user starts the line with cat/dog/llama. 我看到的唯一问题是用户是否以cat / dog / llama开始生产线。 CAT! 猫! Would show up as non profane because there is no space at the start and there is no space afterwards, is there a String command to spread things out or am i completely on the wrong path? 会以亵渎的形式出现,因为开始时没有空间,事后也没有空间,是否有String命令将内容散布开或我完全走错了路?

import java.util.Scanner;
public class ProfanityFilter 
{

 public static void main(String[] args) 
 {

   System.out.println (" welcome to the Profanity filter billboard service ");
   System.out.println (" please enter  a potential sign that contains the words \"cat\", \"dog\" or \"llama\"" );

   Scanner keyboard = new Scanner (System.in);

   String s1 = keyboard.nextLine();

   s1 = s1.toLowerCase();

   if (s1.indexOf(" cat ")!=-1 || s1.indexOf(" dog ") !=-1 || s1.indexOf(" llama ")!=-1)

         System.out.println ("Profanity is in your Billboard");
   else
        System.out.println ("There is no profanity in your Billboard");

 }
}

Regex to the rescue! 正则表达式可以解救!

if (s1.matches("^.*\\b(cat|dog|llama)\\b.*$"))

Some explanation: 一些解释:

  • \\b means word boundary \\b表示单词边界
  • (a|b|c) means "a or b or c" (a|b|c)表示“ a或b或c”

You could construct this from an array: 您可以从一个数组构造它:

String[] badWords = {"cat", "dog", "llama"};
String regex = "^.*\\b(" + 
    Arrays.toString(badWords).replace(", ", "|").replace("[", "").replace("]", "") + 
    ")\\b.*$";

看一下Pattern类和\\b ,这意味着单词边界。

您应该使用正则表达式

You could do as follows that way it will work for "cat" and "horse, cat, and goat" as well. 您可以按照以下方式进行操作,使其同样适用于“猫”和“马,猫和山羊”。

if(Regex.IsMatch(s1, @"\b(?:cat|dog|llama)\b", RegexOptions.IgnoreCase))
    System.out.println("Profanity is in your Billboard");

Okay, I thought it was C#, but something similar exists in Java. 好的,我以为是C#,但是Java中也存在类似的东西。

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

相关问题 Java:如何创建10只猫? 当Cat(“cat”+ i)=“cat name”不能像其他语言一样工作时 - Java: how to create 10 cats? when Cat (“cat” + i) = “cat name” not working as in other languages 如何在gradle构建中执行cat文件 - How to do cat file in gradle build 如何在 JGit 中“cat”一个文件? - How to "cat" a file in JGit? 在Windows命令行上,如何将cat的输出输入到java main方法中? - On the Windows command line, how do I feed output from cat into my java main method? 如何从json字符串获取文件“ cat2”的值 - How to Get value of filed “cat2” from the json string 如何查看Android设备的经纬度? - How can I cat latitude and longitude of Android device? 我如何在不需要创作的页面中获取用户名 - How cat I get username in the page that not need authored 类型的哈希表 <String,String> 正在存储 <String, Integer> 键入值,就像我在日志猫和调试器中看到的那样。 可能吗? - Hashtable of type <String,String> is storing <String, Integer> type value as I see in the log cat and debugger. Is it possible? 为什么哈希码对于String s1 =“cat”和String s2 = new String(“cat”)是相同的? - Why hash code is same for String s1= “cat” and String s2= new String(“cat”)? 如何解决 Log cat 错误? 项目 ImageView 示例 - How to solve Log cat Error? Project ImageViewExample
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM