0

I have string like:

This.is.a.great.place.too.work.

(or)

This/is/a/great/place/too/work/

I want to see if this string has the word "place".

If they are word in a string I used contains("place"). as this is all one string I tried split but it is giving syntax error . can you please let me know how to get it?

string.contains("place") works
String.split(".").contains("place")   Error
1
  • 1
    "it is giving syntax error" Copy/paste the error as an edit to the question. Use code formatting. To do that, select the sample and click the {} button above the messaged posting/editing form. Commented Sep 6, 2012 at 6:03

7 Answers 7

2

use string.split("\\.").contains("place") this should work. .(dot) is a reserved character in regular expression, You need to escape that by using \\..

Sign up to request clarification or add additional context in comments.

Comments

2

Try to split with "//."

    String string = "This.is.a.great.place.too.work.";
    System.out.println(string.split("\\.")[0]);

Comments

1

Try it simply like this.......

String s = "This/is/a/great/place/too/work/";
String ss = "This.is.a.great.place.too.work";

System.out.println(s.contains("place"));
System.out.println(ss.contains("place"));

Comments

1

you can use

String str = "This.is.a.great.place.too.work.";
boolean exists = str.contains("place");

This checks to find the word place in a String and returns true if found, false if not

1 Comment

mine is a string like: This.is.a.great.place.too.work. so this wil not work with out split
1

you dont need to split the string in order to use the containts methode.

string str = "This.is.a.great.place.too.work.";

boolean found = str.contains("substring"); // should return true or false

Alternative you can use the:

str.indexOf("substring") > 0 to determine if the substring is in the string

To do this in one single line you can do something like:

Arrays.toString(str.split(".")).Contains("substring")

Comments

1

Besides using the .contains method, which I think is best, you seem to want to build a data structure and search through it. To do something like that, you will need to do this:

String str = "This.is.a.great.place.too.work.";
String[] splitWords = str.split("\\.");  //The . is a special character in regex language, thus needs to be escaped. Alternatively you could use \\W which will split the string where ever it finds a character which is not a letter, digit or underscore.
List<String> words = Arrays.asList(splitWords);
System.out.println(words.contains("place"));

Alternatively, you could use the indexOf(String str) method like so:

if (str.indexOf("place") > 0)
{
    System.out.println("String Exists");
}

Comments

1

try this

string.split(pattern);
String[] strings = s.split(".")

Split will give array of strings which are splitted based on the paramater .

you cannot apply contains on string array , instead use the following

boolean flag = string.contains("required_pattern");

3 Comments

can I not do Split and contains in the same line?>
no , split will give array of strings as output. contains() can be used only on string
You can do something like Arrays.toString(str.split(".")).Contains("substring") but it looks really ugly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.