简体   繁体   English

使用子字符串查找字符串

[英]Using substring to find a string

I want to check if a String of groceries contain any words related to junk food (eg 'chocolate', 'candy', 'crisps') and then I want to change these words to fruits/vegetables. 我想检查一串杂货是否包含与垃圾食品相关的任何单词(例如“巧克力”,“糖果”,“薯片”),然后将这些单词更改为水果/蔬菜。

How can I use the substring/indexOf methods without knowing the location of the words, as it will be user input. 如何在不知道单词位置的情况下使用substring / indexOf方法,因为这将是用户输入。

Thanks. 谢谢。

You can always use String.replace() , but if you must use String.substring() , then the way it is usually done is as follows: 您可以始终使用String.replace() ,但是如果必须使用String.substring() ,则通常采用以下方式:

    String junk = "chocolate";
    String food = "some chocolate bar";
    int index = food.indexOf( junk );
    if( index != -1 )
    {
        int n = index + junk.length();
        food = food.substring( 0, index ) + "banana" + food.substring( n );
    }
    System.out.println( food );

So it is not a "String array", plz edit this. 因此,它不是“字符串数组”,请对此进行编辑。 i have to guess your code ? 我要猜你的代码?

String s = "potato, chocolate, strawberrie"
s.replace("chocolate", "apple");

You can still do it with substring if u want to train yourself. 如果你想训练自己,你仍然可以用子字符串来做。 http://docs.oracle.com/javase/7/docs/api/java/lang/String.html http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

You will need : 你会需要 :

String choco = "chocolate";
int pos = s.indexOf(choco); //returns 8
int size = choco.length;
s = s.substring(0, pos) + "apple" + s.substring(pos+size, s.length);

Here is everything u need :) 这就是你需要的一切:)

What you really want to do is iterate on the array and verifiy if each value of the array contais a mention to a junk food; 您真正想做的是在数组上进行迭代,并验证数组的每个值是否包含对垃圾食品的提及; the method contains is more appropiate for that. 该方法所包含的内容对此更为合适。

boolean junkFoodValidator(String[] array) { for (String a : array{ if(a.contains("chocolate") || a.contains("candy") { //you can add more validados here return true; } } return false; } boolean junkFoodValidator(String [] array){for(String a:array {if(a.contains(“ chocolate”)|| a.contains(“ candy”){//您可以在此处添加更多有效值,返回true;}}返回false;}

I don't think substring is a "good" way to do this as if you look at the API for String, there isn't a substring method that takes parameters other than indices of characters in the string. 我不认为子字符串是执行此操作的“好方法”,就好像您查看String的API一样 ,没有一个子字符串方法可以使用参数,而不是字符串中字符的索引。 But you will still need substring! 但是您仍然需要子字符串!

Assuming you still want to do this in a manner where you dump the array into a string, a more suitable method of the String API would be indexOf(). 假设您仍希望以将数组转储到字符串中的方式执行此操作,则String API的更合适方法是indexOf()。 The parameter is the string you want to look for and the method will return the index of the first occurrence or a -1 if it is not found. 参数是您要查找的字符串,该方法将返回第一次出现的索引,如果未找到则返回-1。 Once you have the initial index of the string from indexOf(), then you can use substring to get the string. 一旦从indexOf()获得了字符串的初始索引, 可以使用子字符串来获取字符串。 For the second parameter in the substring method, the end index, you can just add the length of the String you're looking for, minus 1, to the start index. 对于substring方法中的第二个参数,即结束索引,您只需将要查找的String的长度减去1,就添加到开始索引中。

If you want to do this from a more object oriented approach, I recommend creating an object for each String and then you can customize that class of object to have a parameter holding what type of item the String is. 如果要通过面向对象的方法来执行此操作,建议您为每个String创建一个对象,然后可以自定义该对象的类,以使用一个参数保存String是什么类型的项目。 Then, using the comparator, you can iterate through the array finding types of items. 然后,使用比较器,可以遍历数组以查找项的类型。 I provided an in depth answer on how to do this in this question . 我对这个问题的解决方法提供了深入的解答。

Give one of these a shot and if you're still stuck or having difficulties, comment on my answer and I can add an edit with some code. 试一试,如果您仍然遇到困难或遇到困难,请评论我的答案,然后添加一些代码进行编辑。 But it's important that you try first! 但是,请务必首先尝试!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM