JAVA – string methods – 1

Java is rich with methods to handle strings and strings manipulation. In this post I will detail the most used string methods and usage of each one. لعبة الاحجار الفرعونية As string functions are much and important I will make this post divided to several posts in a series to be easy to follow and to give time to exercise.

length()

This method Returns the length of the sequence of characters represented by this object.

Syntax:

public int length();

Example:

public class Sample{

   public static void main(String args[]){

      String Str1 = new String("Hello Sami");

      String Str2 = new String("Welcome John" );

      System.out.print("First String Length :" );

      System.out.println(Str1.length());

      System.out.print("Second String Length :" );

      System.out.println(Str2.length());

   }

}

Result:

First String Length :10

Second String Length :12

substring()

This method has two deferent inputs one integer or two integer parameters as character indexes. The method with only one parameter returns a part of the string starting from the integer index. لعبة كوتشينة اون لاين The method with only two parameters returns a part of the string starting from the first integer index and ends before the second index value. نتائج كوبا امريكا 2023

Syntax:

public String substring(int i)

or

public String substring(int i, int e)

i  the begin index, inclusive.

e  the end index, exclusive.

Example:

public class Sample{

   public static void main(String args[]){

      String Str = new String("Hello Sami");

      System.out.println(Str.substring(6) );

      System.out.println(Str.substring(6, 9) );

   }

}

Result:

Sami

Sam

toLowerCase()

This method changes the case condition of the characters to lower case condition. This functions have an optional parameter of the localization attribute used to convert to localized lowercase

Syntax:

public String toLowerCase([Locale locale])

Example:

public class Sample{

   public static void main(String args[]){

      String Str = new String("HELLO SAMI");

      System.out.println(Str.toLowerCase());

   }

}

Result:

hello sami

toUpperCase()

This method changes the case condition of the characters to upper case condition. This functions have an optional parameter of the localization attribute used to convert to localized uppercase.

Syntax:

public String toUpperCase([Locale locale])

Example:

public class Sample{

   public static void main(String args[]){

      String Str = new String("hello sami");

      System.out.println(Str.toUpperCase() );

   }

}

Result:

HELLO SAMI

toString()

Returns a string representation of the object. In general, the method returns a string that “textually represents” this object.

Syntax:

public String toString()

Example:

public class Test {
	public static void main(String args[]) {
		String str1 = new String("Hello Sami");
		System.out.println(str1.toString()); 
	}
}

Result:

Hello Sami

​trim()

This method returns a copy of the string, with removing leading and trailing whitespaces.

Syntax:

public String trim()

Example:

public class Sample{

   public static void main(String args[]){

      String Str = new String("   Hello Sami   ");

      System.out.println(Str.trim() );

   }

}

Result:

Hello Sami

Comments on this post

No comments.

Leave a Reply

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

8 + 17 =

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Trackbacks and Pinbacks on this post

No trackbacks.

TrackBack URL