instanceof java
Posts: 576
Nickname: instanceof
Registered: Jan, 2015
instanceof java is a java related one.
How to get first two characters of a string in java
Posted: May 6, 2017 5:48 AM
This post originated from an RSS feed registered with Java Buzz
by instanceof java.
Original Post: How to get first two characters of a string in java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java
Advertisement
We can get first two character of a string in java by using subString() method in java To get first N numbers of a string s.substring(0,n); Lets see an example java program on how to get first two characters or first N characters.
Program #1: Java Program to get first two characters of a String. package StringInterviewprograms; public class GetFirstTwoCharacters { /** * How to get First N characters in java * @author www.instanceofjava.com */ public static void main(String[] args) { String str="Get first two characters of a String"; System.out.println(str.substring(0,2)); } } Output:
Program #2: package StringInterviewprograms; import java.util.Scanner; public class GetFirstTwoCharacters { /** * How to get First N characters in java * @author www.instanceofjava.com */ public static void main(String[] args) { Scanner sc= new Scanner(System.in); System.out.println("Please Enter a String"); String str=sc.next(); if(str.length()>=2){ System.out.println(str.substring(0,2)); }else{ System.out.println("please enter a string with minimum length of 2."); } } } Output: Please Enter a String miss you mi
Read: How to get first two characters of a string in java