examples of String class library in java


String class library contains tens of methods to help java programmer handle alot of complex tasks with strings.

here are examples of using it:

All Contributions

example of chartAt() method in java:

import java.util.Scanner;
public class Main
	public static void main(String[] args) {
	    Scanner input=new Scanner(System.in);
	    System.out.println("please enter the first 12 digits of an ISBN-13 as a string : ");
	    String isbn=input.next();
	    int checksum= 10-((int)isbn.charAt(0)+3*isbn.charAt(1)+isbn.charAt(2)+3*isbn.charAt(3)+isbn.charAt(4)+3*isbn.charAt(5)+isbn.charAt(6)+3*isbn.charAt(7)+isbn.charAt(8)+3*isbn.charAt(9)+isbn.charAt(10)+3*isbn.charAt(11)%10);
	    System.out.println("the standard ISBN-13 IS : "+isbn);

Write a java program which prompts the user to enter a vehicle plate number in the format  s-LLL-DDDD, where L is an UPPERCASE LETTER and D is a digit, and s equals: 

■ KSA: for Saudi vehicle plate number

■ USA: for American vehicle plate number 

■ UK: for British vehicle plate number


Your program should:

Verify if the vehicle plate number is valid.

Give the nationality of the vehicule.


Here are sample runs:

Enter a vehicle plate number: KSA-ABC-2357 is a valid Saudi vehicle plate number

Enter a vehicle plate number: USA-ABC-2357 is a valid American vehicle plate number

Enter a vehicle plate number: 562-dFe-7896 is an invalid vehicle plate number



import java.util.Scanner;
public class Main
    /** Return true if the vehicule plate number is valid */
public static boolean isValid(String plate)
    String parts[]=plate.split("-",3);
    if(!(parts[0].equals("KSA") || parts[0].equals("USA") || parts[0].equals("UK")))
    return false;
    return false;
    char charArray[]= parts[1].toCharArray();
    for(int i=0;i<charArray.length;i++)
    return false;
    return false;
    charArray= parts[2].toCharArray();
    for(int i=0;i<charArray.length;i++)
    return false;
    return true;
/** Return the nationality of the vehicule */
public static String getNationality(String nationality)
    return "Saudi";
    else if(nationality.equals("USA"))
    return "American";
    else if(nationality.equals("UK"))
    return "United Kingdom";
    else return "";
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter a vehicle plate number:");
		String plate=input.next();
		boolean v=isValid(plate);
		System.out.print("is a valid ");
		else System.out.print("is an invalid ");
		String parts[]=plate.split("-",2);
		System.out.println(getNationality(parts[0])+" vehicle plate number");

total contributions (2)

New to examplegeek.com?

Join us