The Artima Developer Community
Sponsored Link

Java Buzz Forum
How to get first two characters of a string in java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
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
Reply to this message Reply

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.



  1.  package StringInterviewprograms;
  2.  
  3. public class GetFirstTwoCharacters {
  4.  
  5. /**
  6. * How to get First N characters in java 
  7. * @author www.instanceofjava.com
  8. */
  9.  
  10. public static void main(String[] args) {
  11.         
  12.         String str="Get first two characters of a String";
  13.         
  14.         System.out.println(str.substring(0,2));
  15. }
  16.     
  17.     
  18. }
     
Output:

  1.  Ge

  Program #2:


  1. package StringInterviewprograms;
  2. import java.util.Scanner;
  3.  
  4. public class GetFirstTwoCharacters {
  5. /**
  6. * How to get First N characters in java 
  7. * @author www.instanceofjava.com
  8. */
  9. public static void main(String[] args) {
  10.         
  11.          Scanner sc= new Scanner(System.in);
  12.          
  13.          System.out.println("Please Enter a String");
  14.          
  15.          String str=sc.next();         
  16.         if(str.length()>=2){
  17.  
  18.             System.out.println(str.substring(0,2));
  19.         }else{
  20.             System.out.println("please enter a string with minimum length of 2.");
  21.         }
  22.         }
  23.  
  24.   }

 Output:


  1. Please Enter a String
  2. miss you
  3. mi


Get First Two characters String Java

Read: How to get first two characters of a string in java

Topic: How to check if first character of a string is a number or not in java Previous Topic   Next Topic Topic: 40% off Dell Computer Ultrasharp U2415 24.0-Inch Screen LED Monitor - Deal Alert

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use