wa4_tasty_elephant
wa4_tasty_elephant

Reputation: 157

How can I check for no whitespace before and after a character?

How can I check if there is no whitespace before and after a dot?

Example output's:

ABCSF.GHJJKA = true;
ABCSF . GHJJKA = false;
ABCSF. GHJJKA = false;
ABCSF .GHJJKA = false;
ABCSFGHJJKA = false;
ABCSF GHJJKA = false;

I use example.matches();

Edit:

AB CSF.GHJ JKA = true;
AB CSF.GHJJKA = true;
ABCSF.GHJ JKA = true;

Upvotes: 1

Views: 357

Answers (2)

faizal vasaya
faizal vasaya

Reputation: 492

Here is the code that will help you determine a whitespace in a string.

WhiteSpace.java

public class WhiteSpace {

public static void main(String[] args) {

    String test_str = "My.name.is.anthony"; //Declare and define a string

    boolean flag = false ; // Initialize boolean variable flag to false

    //For loop that iterates through the entire string 
    for(int  i = 0 ; i < test_str.length() ; i++ ){

        if(Character.isWhitespace(test_str.charAt(i))){ //check whether the current character is a white space

            flag = true;
            break;  //Breaks as soon as the first white space is encountered
        }
    }
    if(flag == true ){

        System.out.println("The string has a white space!!");

    }else{
        System.out.println("No white space!!");
    }
}
}

Upvotes: 0

Thanga
Thanga

Reputation: 8101

Use the below regex to match your requirement

 [\w]+\.[\w]+

Upvotes: 1

Related Questions