How to use "if" for a condition and ignore a word in a specific sentence?

How can I make the condition that if the phrase has the name "x", then "x" is ignored when the phrase is displayed?

Example:

if(item.contains("Text"))
{
    //Then ignore "Text" and display the remaining mill
}
+4
source share
4 answers

You can easily use:

String item = "This is just a Text";
if (item.contains("Text")) {
    System.out.println(item.replace("Text", ""));
}
+6
source

here, replace () . public String replace (char oldChar, char newChar)

Parameters:

oldChar: old character

newChar: new character

public class ReplaceExample1{  
    public static void main(String args[]){  
        String s1="stackoverflow is a very good website";  
        String replaceString=s1.replace('a','e');//replaces all occurrences of 'a' to 'e'  
        System.out.println(replaceString);  
    }
}  

O / P:

steckoverflow is e very good website
+4
source

indexOf()

String val = "How can I construct a condition that if a phrase ";
String valFinal = val.indexOf("that") != -1 ? val.replace("that", "") : val;
System.out.println(valFinal);
+3

, :

    String x = "This is Text";
    String[] words;
    String newX = "";

    words = x.split(" ");
    for(int i = 0; i < words.length; i++) {
            if(!words[i].equals("Text"))
                newX = newX + " " + words[i];
    }

    System.out.println(newX);
+2

Source: https://habr.com/ru/post/1675839/


All Articles