Rahmadnet

String

Jun 29th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. * String
  2.  
  3. package Strings;
  4.  
  5. public class TestClassString
  6. {
  7.  
  8.     public static void main(String[] args)
  9.     {
  10.         String s = "RAHMAD NASUTION";
  11.         // or String s = new String ("RAHMAD NASUTION");
  12.        
  13.         // Returns the number of characters in the String
  14.         System.out.println("String lenght = " + s.length());
  15.        
  16.         // Returns the character at ith index.
  17.         System.out.println("Character at 3rd position = " + s.charAt(3));
  18.        
  19.         // Returns the substring from the ith index character
  20.         // to end of string
  21.         System.out.println("Subsstring " + s.substring(3));
  22.        
  23.         // Returns the substring from i to j-1 index.
  24.         System.out.println("Substring = " + s.substring(2,5));
  25.        
  26.         // Concatenates string2 to the end string1
  27.         String s1 = "Rahmad";
  28.         String s2 = "Nasution";
  29.         System.out.println("Concatenated string = " + s1.concat(s2));
  30.        
  31.         // Returns the index within the string
  32.         // of the first occurrence of the specified string.
  33.         String s4 = "Learn Share Learn";
  34.         System.out.println("Index of share " + s4.indexOf("Share"));
  35.        
  36.         // Returns the index within the string of the
  37.         // first occurence of the specified string,
  38.         // string at the specified index.
  39.         System.out.println("Index of a = " + s4.indexOf('a',3));
  40.        
  41.         // Checking equality of String
  42.         Boolean out = "Rahmad".equals("Rahmad");
  43.         System.out.println("Checking Equality " + out);
  44.         out = "Rahmad".equals("Rahmad");
  45.         System.out.println("Checking Equality " + out);
  46.        
  47.         out = "Rahmad".equalsIgnoreCase("Rahmad");
  48.         System.out.println("Checking Equality " + out);
  49.        
  50.         int out1 = s1.compareTo(s2);
  51.         System.out.println("If s1 = s2 " + out);
  52.        
  53.         // Converting cases
  54.         String word1 = "Playing Football";
  55.         System.out.println("Changing to Lower Case " + word1.toLowerCase());
  56.        
  57.         // Trimming the word
  58.         String word2 = "Bassket Ball";
  59.         System.out.println("changing to Upper Case " + word1.toUpperCase());
  60.        
  61.         // Triming the word
  62.         String word4 = "Learn to work Sharing";
  63.         System.out.println("Trim the word " + word4.trim());
  64.        
  65.         // Replacing characters
  66.         String str1 = "feeksforfeeks";
  67.         System.out.println("Original String " + str1);
  68.         String str2 = "feeksforfeeks".replace('f', 'g');
  69.         System.out.println("Replaced f with g -> " + str2);
  70.        
  71.     }
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment