---
title: "Comparing Strings With Variable White Spacing"
description: "Stings may sound like 101 of programming, but just a glimpse of TAOCP would turn the thought inside out! Comparing Strings."
url: "https://h3manth.com/scribe/comparing-strings-variable-white-spacing/"
canonical_url: "https://h3manth.com/scribe/comparing-strings-variable-white-spacing/"
markdown_url: "https://h3manth.com/scribe/blog/comparing-strings-variable-white-spacing.md"
publishDate: "2012-04-12T00:00:00.000Z"
author: "Hemanth HM"
category: "python"
tags: ["ruby","python","javascript"]
word_count: 367
reading_time_minutes: 2
x_markdown_tokens: 679
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# Comparing Strings With Variable White Spacing

> Stings may sound like 101 of programming, but just a glimpse of TAOCP would turn the thought inside out! Comparing Strings.

Stings may sound like 101 of programming, but just a glimpse of TAOCP would turn the thought inside out!

Comparing Strings is on of the most common tasks that any programmer would do, but the fun never ends with strings!

Comparing Strings with variable white spacing, is also one among the most common cases, below /me has tried to collect the most intuitive way of doing it in **Ruby, Perl, Python and Javascript**

Say we have two strings **str1="hemanth is testing"** and  **str2="hemanth is testing"** with white spaces in consideration, these two strings are not equal, but logically handling the extra spaces would reveal that these strings are equal, which plays a very important role in fileds like : parsing, NPL, testing and so forth.

On the other hand, if we try to compare strings ignoring cases, we will end up with wrong results, say for example comparing two strings like "a book" and "ab o ok" ignoring spaces would be the same, but they are in real not!

The below are the ways to compare strings with variable strings are as below :

**Ruby :** Out of all the languages considered here, /me selected ruby because, none of the other languages provide such an easy way to do this with their own standard libs.

>> "a book".squeeze == "a book".squeeze  
=> true  

**Perl :****Perl's R.E are always interesting.

$str1 = "hemanth is testing" ;  

**

  

$str2 = "hemanth is testing" ;  

  

$str1 =~ [tr](http://perldoc.perl.org/functions/tr.html) / //[s](http://perldoc.perl.org/functions/s.html);  

  

$str1 == $str2 ? [print](http://perldoc.perl.org/functions/print.html) 1 : [print](http://perldoc.perl.org/functions/print.html) 0;   

  

**Python:**Indeed the famous "import re"****

>>> s1="hemanth is testing"  
>>> s2="hemanth is testing"  
>>> re.sub("[\x00-\x20]+", " ",s2) == s1  
True  
  
  

**JavaScript**

str1 = "hemanth is testing"  
str2 = "hemanth is testing"  
str1.replace(/\s+/g, " ") == str2.replace(/\s+/g, " ")

Do let me know your ways of doing the same!

_Update 0:_

As per the comments below from Kevin, it worth to keep this is mind :

```bash
 1.9.3-p327 :001 > "a book".squeeze == "a bok".squeeze => true 1.9.3-p327 :002 > "a book".squeeze(" ") == "a book".squeeze(" ") => true 1.9.3-p327 :003 > "a book".squeeze(" ") == "a bok".squeeze(" ") => false 
```

P.S : We are passing an empty sting to squeeze!
