0

I am trying to find the occurrence of hyphen - in a string "TC 1 - TC 24". For that I used

"TC 1 - TC 24".contains(" \\-")

But the above expression doesn't returns true. What am I doing wrong?

The code looks like:

if("TC 1 - TC 24".contains(" \\- ") == true) {

   //print something
}
Nicholas K
  • 14,695
  • 7
  • 31
  • 56
  • 1
    Post your attempt code please – bakero98 Aug 29 '18 at 17:30
  • 4
    *Hint:* The parameter to [`contains()`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#contains-java.lang.CharSequence-) is not a regex, it's a literal value. Use `contains(" - ")`. --- Also, even if the parameter had been a regex, you still wouldn't need the ``\\``, since a `-` is not a special character in a regex outside a `[]` character class. – Andreas Aug 29 '18 at 17:32
  • Possible duplicate of [Occurrences of substring in a string](https://stackoverflow.com/questions/767759/occurrences-of-substring-in-a-string) –  Aug 29 '18 at 17:33
  • 1
    *Hint:* Using `== true` is unnecessary. See [Is it bad to explicitly compare against boolean constants e.g. if (b == false) in Java?](https://stackoverflow.com/q/2661110/5221149) – Andreas Aug 29 '18 at 17:39
  • @weizenkeimhugo I don't see how that is a duplicate of this question. – Mark Rotteveel Aug 31 '18 at 16:53

4 Answers4

1

Contains keyword in java returns true if provided string contains the specified sequence of char values

So, the below string returns True as it contains hyphen. But, we cannot count the no. of times hyphen occurs.

if ("TC 1 - TC- 24".contains("-")) {
    System.out.println("It containes hyphen ");
}

I am finding occurrence of hyphen using the below code:

public static int findCharInString(String input,char inputChar) {
    int counter=0;
    for (int i = 0; i < input.length(); i++) {
        if(input.charAt(i)==inputChar) {
            counter++;
        }
    }
    return counter;
}
Nicholas K
  • 14,695
  • 7
  • 31
  • 56
0

You could use something like:

if ( yourString.matches("[\\p{Alnum} ]+ - [\\p{Alnum} ]+") )
{
    System.out.println("String contains a hyphen")
}

the \\p is a POSIX character class for Alphanumeric

spectrum
  • 319
  • 3
  • 11
0

You can use the indexOf method of String class, as indexOf searches the String from given String. If it finds then it will return the index of that String otherwise it will return -1. So you can easily use indexOf. I have given below code please go through with it.

public static void main(String[] args) {
    String hypen="12-35";
    int index = hypen.indexOf("-");
    if (index != -1) {
        System.out.println("True");
    } else {
        System.out.println("False");
    }
}
Nicholas K
  • 14,695
  • 7
  • 31
  • 56
-1
String str = "TC 1 - TC 24";

int str_length = str.length();
for (int i=0; i<str_length; i++) {
    char ch = str.charAt(i);
    if (ch == '-') {
        System.out.println("True");
        break;
    }
}
Ilario Pierbattista
  • 3,051
  • 2
  • 30
  • 39
wahidd
  • 65
  • 1
  • 5