Checking If a String Contains a Dot

Checking if a string contains a dot

contains() method of String class does not take regular expression as a parameter, it takes normal text.

String s = "test.test";

if(s.contains("."))
{
System.out.println("string contains dot");
}

How to check if a string contains dot (period)?

JavaScript: check if string contains '.' (a full-stop)

Why does that happen?

The reason why _email.search("."); returns 0 every time is because String.prototype.search takes a regular expression as its input.

. means match any character in regex. In other words, if your input has at least one character of anything, it will return 0.


The Solution

If you simply change it to _email.search(/\./);, it will work exactly as you intended.

Browser support: All known browsers

If you don't care about browser support, you may also use _email.includes('.'); as mentioned by Cade Brown.

See here for reference. Browser support: Only Chrome 41+ and Firefox 40+ (recent browsers)

How to check if a String variable contains a dot/period(.) character?

The most simplest is

input.Contains('.')

Swift – How to find out if a string contains several identical characters?

You can use filter(_:) on the string and count to get the number of dots:

let str = "3..14"

switch str.filter({ $0 == "." }).count {
case 0:
print("string has no dots")
case 1:
print("string has 1 dot")
default:
print("string has 2 or more dots")
}

Java: Check if string only contains 1,0 or a dot

String.contains works with Strings NOT with REGEX. use String.matches
instead. As @lazy points out, you could use Pattern and Matcher classes as well.

test.contains("[^10\\.]") == true 

All you are doing here is checking whether test contains the String literals [^10\.]

Regex to match the string which contains dot

You need to use positive lookahead

let str  = `.row`
console.log(str.match(/\.(?=[A-Za-z])/g))

How to check if a string contains a dot followed by a number in PHP

Ok, here is a working solution:

$string = "this text contains the specified format: .233 and some nonsense.";
preg_match('/^.*(\.\d+).*$/m', $string, $matches);

var_dump($matches);

//returns
array(2) {
[0]=>
string(64) "this text contains the specified format: .233 and some nonsense."
[1]=>
string(4) ".233"
}

see the example at: http://regex101.com/r/eF0cU7/1



Related Topics



Leave a reply



Submit