beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
    • Learn jQuery
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java String trim() and hashCode() Methods with examples

By Chaitanya Singh | Filed Under: String handling

In this  tutorial we will discuss Java String trim() and hashCode() methods with the help of examples.

Java String trim() method signature

It returns a String after removing leading and trailing white spaces from the input String. For e.g. "     Hello".trim() would return the String "Hello".

public String trim()

Java String trim() method example

In the following example we have a string with leading and trailing white spaces, we are using trim() method to get rid of these leading and trailing white spaces but we want to retain the white spaces that are in between the words of the given string str. The trim() method only removes the leading and trailing white spaces and leaves the in between spaces as it is.

public class JavaExample{
   public static void main(String args[]){
	String str = new String("    How are you??   ");
	System.out.println("String before trim: "+str);
	System.out.println("String after trim: "+str.trim());
   }
}

Output:
Java String trim() method example

Java String hashCode() method signature

This method returns the hash code for the String. The computation is done like this:

s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
public int hashCode()

Java hashCode() method Example

In the following example we have a string str with the value “Welcome!!”, we are displaying the hash code of this value using the hashCode() method.

public class JavaExample{
   public static void main(String args[]){
	String str = new String("Welcome!!");
	System.out.println("Hash Code of the String str: "+str.hashCode());	      
   }
}

Output:
Java String hashCode() method example

References:

  1. HashCode JavaDoc
  2. hashCode() JavaDoc
  3. trim() JavaDoc
❮ PreviousNext ❯

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorials

  • Learn Java
  • OOPs Concepts
  • Java Collections

Java String

  • Java String

Java String Methods

  • String charAt()
  • String compareTo()
  • String compareToIgnoreCase()
  • String contains()
  • String concat()
  • substring
  • String valueOf()
  • String startsWith()
  • String equals()
  • String format()
  • String endsWith()
  • String indexOf()
  • String lastIndexOf()
  • String length()
  • String replace()
  • String split()
  • String trim()
  • String intern()
  • String isEmpty()
  • String matches()
  • String regionMatches()
  • String contentEquals()
  • String toCharArray()
  • String getBytes()
  • String join()
  • String getChars()
  • String copyValueOf()

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap