Return true if string cointains "xyz" not preceeded by a period?

string contains python
java string contains char
string contains java
java string contains ignorecase
java check if string contains characters
string.contains c#
string contains javascript
java string contains regex

I'm trying to solve this CodingBat problem:

Return true if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz" does not.

xyzThere("abcxyz") → true
xyzThere("abc.xyz") → false
xyzThere("xyz.abc") → true

My attempt:

public boolean xyzThere(String str) {
  boolean res = false;

  if(str.contains(".xyz") == false && str.contains("xyz")){
    res = true;
  }

  return res;

}

The problem is that is passes all the tests except the one below because it contains two instances of xyz:

xyzThere("abc.xyzxyz")

How can I make it pass all tests?

public boolean xyzThere(String str) {
    return(!str.contains(".xyz") && str.contains("xyz"));
}

Edit: Given that ".xyzxyz" should return true, the solution should be:

public boolean xyzThere(String str) {
    int index = str.indexOf(".xyz");
    if(index >= 0) {
        return xyzThere(str.substring(0, index)) || xyzThere(str.substring(index + 4));
    } else return (str.contains("xyz"));
}

Java String contains() method explained with examples, As you can see, the in operator returns True when the substring exists in the string. Otherwise, it returns false. This method is very straightforward,  Return value. TRUE if find_text is a substring of within_text; otherwise FALSE. Remarks. CONTAINSSTRING is not case-sensitive. You can use ? and * wildcard characters. Use ~ to escape wildcard characters. Example. DAX query

public static boolean xyzThere(String str) {
    int i = -1;
    while ((i = str.indexOf("xyz", i + 1 )) != -1) {
        if (i == 0 || (str.charAt(i-1) != '.')) {
            return true;
        }
    }
    return false;
}

How to Check if a Python String Contains Another String?, It returns true if sequence of char values are found in this string otherwise returns false. Implementation of this method : public boolean contains(CharSequence  Contains(Char) Returns a value indicating whether a specified character occurs within this string. Contains(String) Returns a value indicating whether a specified substring occurs within this string. Contains(String, StringComparison) Returns a value indicating whether a specified string occurs within this string, using the specified comparison rules.

Alternatively, you could replace all occurrences of ".xyz" in the string with "", then use the .contains method to verify that the modified string still contains "xyz". Like so:

return str.replace(".xyz", "").contains("xyz");

Java String contains() method with example, Instead of returning TRUE or FALSE, the formula above, will return "Yes" if substring is found and "No" if not. With hardcoded search string. To test a cell for a  You can use the in operator or the string’s find method to check if a string contains another string. The in operator returns True if the substring exists in the string. Otherwise, it returns False. The find method returns the index of the beginning of the substring if found, otherwise -1 is returned.

Ok, I know everyone is eager to share their expertise but straight giving the kid the answer does little good.

@EnTHuSiAsTx94

I was able to pass all of the tests with three statements. Here is a hint: Try using the string replace method. Here is the method signature:

String replace(CharSequence target, CharSequence replacement)

On a minor note, the first condition in your if statement can be simplified from:

str.contains(".xyz") == false

to:

!str.contains(".xyz")

The contains method already returns true or false, so there is no need for the explicit equals comparison.

Excel formula: Cell contains specific text, The in operator returns true if the substring exists in a string and false if ​​otherwise. Syntax. The general syntax is: substring in string. In Java, I want to check whether a String exists in a List<String> myList. The problem is myList can contain un-trimmed data: I want it to return true if my item 'B' is in the list. How should I do this? I would like to avoid a looping structure. AFAIK in vanilla JDK, no. I guess in Java 8 with closures you will achieve this in one line.

public boolean xyzThere(String str) {
return str.startsWith("xyz") || str.matches(".*[^.]xyz.*");
}

How to check if Python string contains another string, The | is a metacharacter meaning OR or either. To evaluate the literal string value​, either escape ( \\ ) or place it inside brackets ( [] ) or use the  This should not be the accepted answer, because value will be interpreted as a regex pattern. fixed=TRUE should always be used unless you know the string you are searching for will not happen to look like a regex pattern. Joshua Creek's answer below has a very clear explanation of this, and should be the accepted answer. – bhaller Apr 24 '18 at 23:20

Return true if string contains "|", contains() method. It's provided by the String class itself and is very efficient. The method accepts a CharSequence and returns true if the sequence  @DTest - well yes of course it will return true because the string contains 'are'. If you are looking specifically for the word ARE then you would need to do more checks like, for example, check if there is a character or a space before the A and after the E. – jsherk Nov 14 '12 at 21:35

Java: Check if String Contains a Substring, It returns a Boolean (either True or False ) and can be used as follows: fullstring = "StackAbuse" substring = "tack" if substring in fullstring: print "Found! If you want to do something when a cell contains specific text, you can wrap the formula in an IF statement like this: = IF ( ISNUMBER ( SEARCH ( substring , text )), "Yes" , "No" ) Instead of returning TRUE or FALSE, the formula above, will return "Yes" if substring is found and "No" if not.

Python: Check if String Contains Substring, This method returns true if the string contains the characters, and false if not. Note​: The includes() method is case sensitive. Browser Support. Method. This is based on another formula ( explained in detail here) that checks a cell for a single substring. If the cell contains the substring, the formula returns TRUE. If not, the formula returns FALSE. Because we are giving the SEARCH function more than one thing to look for, in the named range things, it will give us more the one result, in an

Comments
  • Why are you using a for loop and completely ignoring i? You're just going the same thing over and over, for every character in the string. And what should xyzThere("abc.xyzxyz") return?
  • what should be the result for the test case which you mentioned? Does it pass or not?
  • Depending on your philosophical bent, answer to xyzThere("abc.xyzxyz") may be true or false unless the question states if only the first occurrence of xyz/'.xyz' to check. Also, instead of str.contains(".xyz") == false, use !str.contains(".xyz"). And, drop the for loop.
  • My bad, was trying to do it some other way, forgot to delete that line.
  • @nommyravian, it should return 'true', but returns false in my case.
  • No, the thing is that your code returns the same output as mine, i.e. returning false for xyzThere("abc.xyzxyz"), however, it should return true.
  • Ah, gotcha. I'll edit my answer accordingly. The new answer will strip off all instances of ".xyz" and will then return true of the string still contains an "xyz"
  • Try again, I've changed "str.substring(index)" to "str.substring(index + 4)" which should eliminate the stack overflows
  • If you have the regexp hammer, every problem looks like a nail. Except (HT|X)ML
  • Why wouldn't you just use a negative lookbehind? "(?<!\\.)xyz"
  • Also possible, but not necessary. But probably better (to explain).
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
  • Well this does run all the test cases. Do test it before commenting and ignorantly down-voting.
  • This solution does not pass the user's first 3 tests.
  • Could you provide any clarification or explanation of your code?