Comment puis-je trouver des espaces dans une chaîne?
Question
Comment puis-je vérifier si une chaîne contient un caractère des espaces, un espace vide ou « ». Si possible, s'il vous plaît donner un exemple Java.
Par exemple: String = "test word";
La solution
Pour vérifier si une chaîne contient des espaces blancs utiliser Matcher
et appeler sa méthode de recherche.
Pattern pattern = Pattern.compile("\\s");
Matcher matcher = pattern.matcher(s);
boolean found = matcher.find();
Si vous voulez vérifier si ne se compose d'espaces vous pouvez utiliser String.matches
:
boolean isWhitespace = s.matches("^\\s*$");
Autres conseils
Vérifier si une chaîne contient au moins un caractère d'espace blanc:
public static boolean containsWhiteSpace(final String testCode){
if(testCode != null){
for(int i = 0; i < testCode.length(); i++){
if(Character.isWhitespace(testCode.charAt(i))){
return true;
}
}
}
return false;
}
Référence:
Utilisation de la Goyave bibliothèque, il est beaucoup plus simple:
return CharMatcher.WHITESPACE.matchesAnyOf(testCode);
CharMatcher.WHITESPACE
est aussi beaucoup plus approfondie en matière de prise en charge Unicode.
Cela dira si vous il y a any espaces blancs:
Soit en boucle:
for (char c : s.toCharArray()) {
if (Character.isWhitespace(c)) {
return true;
}
}
ou
s.matches(".*\\s+.*")
Et StringUtils.isBlank(s)
vous dira s'il y a que whitepsaces.
Utilisez Apache Commons StringUtils :
StringUtils.containsWhitespace(str)
Utilisez ce code, est la meilleure solution pour moi.
public static boolean containsWhiteSpace(String line){
boolean space= false;
if(line != null){
for(int i = 0; i < line.length(); i++){
if(line.charAt(i) == ' '){
space= true;
}
}
}
return space;
}
public static void main(String[] args) {
System.out.println("test word".contains(" "));
}
import java.util.Scanner;
public class camelCase {
public static void main(String[] args)
{
Scanner user_input=new Scanner(System.in);
String Line1;
Line1 = user_input.nextLine();
int j=1;
//Now Read each word from the Line and convert it to Camel Case
String result = "", result1 = "";
for (int i = 0; i < Line1.length(); i++) {
String next = Line1.substring(i, i + 1);
System.out.println(next + " i Value:" + i + " j Value:" + j);
if (i == 0 | j == 1 )
{
result += next.toUpperCase();
} else {
result += next.toLowerCase();
}
if (Character.isWhitespace(Line1.charAt(i)) == true)
{
j=1;
}
else
{
j=0;
}
}
System.out.println(result);
Utilisez org.apache.commons.lang.StringUtils.
- pour rechercher des espaces blancs
booléen withWhiteSpace = StringUtils.contains ( "mon nom", " « );
- Pour supprimer tous les espaces blancs dans une chaîne
StringUtils.deleteWhitespace (null) = null StringUtils.deleteWhitespace ( "") = "" StringUtils.deleteWhitespace ( "abc") = "abc" StringUtils.deleteWhitespace ( "ab c") = "abc"
String str = "Test Word";
if(str.indexOf(' ') != -1){
return true;
} else{
return false;
}
Je but de vous une méthode très simple qui utilisent String.contains
:
public static boolean containWhitespace(String value) {
return value.contains(" ");
}
Un petit exemple d'utilisation:
public static void main(String[] args) {
System.out.println(containWhitespace("i love potatoes"));
System.out.println(containWhitespace("butihatewhitespaces"));
}
Sortie:
true
false
Vous pouvez en principe faire
if(s.charAt(i)==32){
return true;
}
Vous devez écrire char method.Whitespace booléenne est 32.
Vous pouvez utiliser chatAt () fonction pour connaître les espaces dans la chaîne.
public class Test {
public static void main(String args[]) {
String fav="Hi Testing 12 3";
int counter=0;
for( int i=0; i<fav.length(); i++ ) {
if(fav.charAt(i) == ' ' ) {
counter++;
}
}
System.out.println("Number of spaces "+ counter);
//This will print Number of spaces 4
}
}
package com.test;
public class Test {
public static void main(String[] args) {
String str = "TestCode ";
if (str.indexOf(" ") > -1) {
System.out.println("Yes");
} else {
System.out.println("Noo");
}
}
}