Check if string contains only spaces
Here given code implementation process.
/*
Java Program for
Check if string contains only spaces
*/
public class Whitespaces
{
public void isAllSpaces(String text)
{
boolean status = false;
// Get the length
int n = text.length();
if (n != 0 && text.trim().length() == 0)
{
status = true;
}
System.out.println(" Given text : ["+text+"]");
if(status)
{
System.out.println(" This are contain all spaces");
}
else
{
System.out.println(" This are not contain all spaces");
}
}
public static void main(String[] args)
{
Whitespaces task = new Whitespaces();
// Test
task.isAllSpaces(" ");
task.isAllSpaces(" _ ");
task.isAllSpaces(" 1");
task.isAllSpaces(" ");
}
}
Output
Given text : [ ]
This are contain all spaces
Given text : [ _ ]
This are not contain all spaces
Given text : [ 1]
This are not contain all spaces
Given text : [ ]
This are contain all spaces
// Include header file
#include <iostream>
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>
#include <string>
using namespace std;
/*
C++ Program for
Check if string contains only spaces
*/
class Whitespaces
{
// Trim from start in place
void ltrim(std::string &s) {
s.erase(s.begin(), find_if(s.begin(), s.end(),
not1(ptr_fun<int, int>(isspace))));
}
// Trim from end in place
void rtrim(string &s) {
s.erase(find_if(s.rbegin(), s.rend(),
not1(ptr_fun<int, int>(isspace))).base(), s.end());
}
string trim(string s) {
ltrim(s);
rtrim(s);
return s;
}
public: void isAllSpaces(string text)
{
bool status = false;
// Get the length
int n = text.length();
if (n != 0 && trim(text).length() == 0)
{
status = true;
}
cout << " Given text : [" << text << "]" << endl;
if (status)
{
cout << " This are contain all spaces" << endl;
}
else
{
cout << " This are not contain all spaces" << endl;
}
}
};
int main()
{
Whitespaces *task = new Whitespaces();
// Test
task->isAllSpaces(" ");
task->isAllSpaces(" _ ");
task->isAllSpaces(" 1");
task->isAllSpaces(" ");
return 0;
}
Output
Given text : [ ]
This are contain all spaces
Given text : [ _ ]
This are not contain all spaces
Given text : [ 1]
This are not contain all spaces
Given text : [ ]
This are contain all spaces
// Include namespace system
using System;
/*
Csharp Program for
Check if string contains only spaces
*/
public class Whitespaces
{
public void isAllSpaces(String text)
{
Boolean status = false;
// Get the length
int n = text.Length;
if (n != 0 && text.Trim().Length == 0)
{
status = true;
}
Console.WriteLine(" Given text : [" + text + "]");
if (status)
{
Console.WriteLine(" This are contain all spaces");
}
else
{
Console.WriteLine(" This are not contain all spaces");
}
}
public static void Main(String[] args)
{
Whitespaces task = new Whitespaces();
// Test
task.isAllSpaces(" ");
task.isAllSpaces(" _ ");
task.isAllSpaces(" 1");
task.isAllSpaces(" ");
}
}
Output
Given text : [ ]
This are contain all spaces
Given text : [ _ ]
This are not contain all spaces
Given text : [ 1]
This are not contain all spaces
Given text : [ ]
This are contain all spaces
package main
import "strings"
import "fmt"
/*
Go Program for
Check if string contains only spaces
*/
type Whitespaces struct {}
func getWhitespaces() * Whitespaces {
var me *Whitespaces = &Whitespaces {}
return me
}
func(this Whitespaces) isAllSpaces(text string) {
var status bool = false
// Get the length
var n int = len(text)
if n != 0 && len(strings.Trim(text, " ")) == 0 {
status = true
}
fmt.Println(" Given text : [", text, "]")
if status {
fmt.Println(" This are contain all spaces")
} else {
fmt.Println(" This are not contain all spaces")
}
}
func main() {
var task * Whitespaces = getWhitespaces()
// Test
task.isAllSpaces(" ")
task.isAllSpaces(" _ ")
task.isAllSpaces(" 1")
task.isAllSpaces(" ")
}
Output
Given text : [ ]
This are contain all spaces
Given text : [ _ ]
This are not contain all spaces
Given text : [ 1]
This are not contain all spaces
Given text : [ ]
This are contain all spaces
<?php
/*
Php Program for
Check if string contains only spaces
*/
class Whitespaces
{
public function isAllSpaces($text)
{
$status = false;
// Get the length
$n = strlen($text);
if ($n != 0 && strlen(trim($text)) == 0)
{
$status = true;
}
echo(" Given text : [".$text."]\n");
if ($status)
{
echo(" This are contain all spaces\n");
}
else
{
echo(" This are not contain all spaces\n");
}
}
}
function main()
{
$task = new Whitespaces();
// Test
$task->isAllSpaces(" ");
$task->isAllSpaces(" _ ");
$task->isAllSpaces(" 1");
$task->isAllSpaces(" ");
}
main();
Output
Given text : [ ]
This are contain all spaces
Given text : [ _ ]
This are not contain all spaces
Given text : [ 1]
This are not contain all spaces
Given text : [ ]
This are contain all spaces
/*
Node JS Program for
Check if string contains only spaces
*/
class Whitespaces
{
isAllSpaces(text)
{
var status = false;
// Get the length
var n = text.length;
if (n != 0 && text.trim().length == 0)
{
status = true;
}
console.log(" Given text : [" + text + "]");
if (status)
{
console.log(" This are contain all spaces");
}
else
{
console.log(" This are not contain all spaces");
}
}
}
function main()
{
var task = new Whitespaces();
// Test
task.isAllSpaces(" ");
task.isAllSpaces(" _ ");
task.isAllSpaces(" 1");
task.isAllSpaces(" ");
}
main();
Output
Given text : [ ]
This are contain all spaces
Given text : [ _ ]
This are not contain all spaces
Given text : [ 1]
This are not contain all spaces
Given text : [ ]
This are contain all spaces
# Python 3 Program for
# Check if string contains only spaces
class Whitespaces :
def isAllSpaces(self, text) :
status = False
# Get the length
n = len(text)
if (n != 0 and len(text.strip()) == 0) :
status = True
print(" Given text : [", text ,"]")
if (status) :
print(" This are contain all spaces")
else :
print(" This are not contain all spaces")
def main() :
task = Whitespaces()
# Test
task.isAllSpaces(" ")
task.isAllSpaces(" _ ")
task.isAllSpaces(" 1")
task.isAllSpaces(" ")
if __name__ == "__main__": main()
Output
Given text : [ ]
This are contain all spaces
Given text : [ _ ]
This are not contain all spaces
Given text : [ 1 ]
This are not contain all spaces
Given text : [ ]
This are contain all spaces
# Ruby Program for
# Check if string contains only spaces
class Whitespaces
def isAllSpaces(text)
status = false
# Get the length
n = text.length
if (n != 0 && text.strip.length == 0)
status = true
end
print(" Given text : [", text ,"]", "\n")
if (status)
print(" This are contain all spaces", "\n")
else
print(" This are not contain all spaces", "\n")
end
end
end
def main()
task = Whitespaces.new()
# Test
task.isAllSpaces(" ")
task.isAllSpaces(" _ ")
task.isAllSpaces(" 1")
task.isAllSpaces(" ")
end
main()
Output
Given text : [ ]
This are contain all spaces
Given text : [ _ ]
This are not contain all spaces
Given text : [ 1]
This are not contain all spaces
Given text : [ ]
This are contain all spaces
import scala.collection.mutable._;
/*
Scala Program for
Check if string contains only spaces
*/
class Whitespaces()
{
def isAllSpaces(text: String): Unit = {
var status: Boolean = false;
// Get the length
var n: Int = text.length();
if (n != 0 && text.trim().length() == 0)
{
status = true;
}
println(" Given text : [" + text + "]");
if (status)
{
println(" This are contain all spaces");
}
else
{
println(" This are not contain all spaces");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Whitespaces = new Whitespaces();
// Test
task.isAllSpaces(" ");
task.isAllSpaces(" _ ");
task.isAllSpaces(" 1");
task.isAllSpaces(" ");
}
}
Output
Given text : [ ]
This are contain all spaces
Given text : [ _ ]
This are not contain all spaces
Given text : [ 1]
This are not contain all spaces
Given text : [ ]
This are contain all spaces
/*
Kotlin Program for
Check if string contains only spaces
*/
class Whitespaces
{
fun isAllSpaces(text: String): Unit
{
var status: Boolean = false;
// Get the length
val n: Int = text.length;
if (n != 0 && text.trim().length == 0)
{
status = true;
}
println(" Given text : [" + text + "]");
if (status)
{
println(" This are contain all spaces");
}
else
{
println(" This are not contain all spaces");
}
}
}
fun main(args: Array < String > ): Unit
{
val task: Whitespaces = Whitespaces();
// Test
task.isAllSpaces(" ");
task.isAllSpaces(" _ ");
task.isAllSpaces(" 1");
task.isAllSpaces(" ");
}
Output
Given text : [ ]
This are contain all spaces
Given text : [ _ ]
This are not contain all spaces
Given text : [ 1]
This are not contain all spaces
Given text : [ ]
This are contain all spaces
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment