Home > database >  Can someone please explain what is the use of 32 in this remove spaces from string method? java
Can someone please explain what is the use of 32 in this remove spaces from string method? java

Time:05-11

public static String RemoveSpace(String s){ String str="";

  for(int i=0;i<s.length();i  )
     if(s.charAt(i)!=32)
        str =s.charAt(i);
    
  return str.toLowerCase();

CodePudding user response:

A char is just a number - a numeric type with two bytes. It's often written inside '' marks, but it can be used interchangeably with other numbers. In particular, the value of ' ' (space) is 32, when considered as a number. So when you compare a char with 32, you're just comparing it to ' ', even though 32 is technically four bytes, not just two.

Your code example just appends characters to str whenever they are NOT the same as 32, in other words, when they're not spaces.

CodePudding user response:

https://www.techonthenet.com/ascii/chart.php

In the ASCII table 32 stands for Space.

  • Related