简体   繁体   中英

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;

使用下面的正则表达式来满足您的要求

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

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!!");
    }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM