Bài tập string trong java



Chào bạn đến với phanlamcoder.blogspot.com!

Ở bài viết này chúng ta sẽ cùng test lại những câu lệnh cơ bản nhé!
Ôn tập lại kiến thức về String trong JAVA!

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package stringjava;

import java.util.Locale;

/**
 *
 * @author Nhu Y
 */
public class StringJava {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String s1 = "Toi dep trai nhat, toi biet toi dep trai";

        //VI TRI XUAT HIEN CUOI CUNG TRONG CHUOI

        System.out.println(s1.lastIndexOf("toi"));
        

        //XOA MOT KY TU

        System.out.print(removeCharAt(s1, 2));
        

        //THAY THE CHUOI CON TRONG CHUOI CHA

        System.out.print(s1.replaceFirst("dep trai", "thong minh"));
        

        //HAI CACH DAO CHUOI

            //cach 1
        s1 = new StringBuffer(s1).reverse().toString();
            //cach 2
        String temp = "";
        for(int i = s1.length()-1;i>-1;i--)
        {
            temp += s1.substring(i);
            s1 = s1.substring(0,i);
        }
        s1 = temp;
        System.out.print(s1);
        

        //TIM MOT TU TRONG CHUOI

        s1 = "hoc-lap-trinh-java";
        
        System.out.print(s1.indexOf("java"));

        //CHIA NHO CHUOI, VONG LAP FOREACH

        
        String[] arrStr = s1.split("-");
        for(String s : arrStr)
        {
            System.out.print(s+" ");
        }

        //SO SANH HAI VUNG CUA CHUOI

        //str1[14-17] == str2[3,6] ???
        String str1 = "Welcome to my blog";
        String str2 = "My blog is lovely";
        //str1[14-17] = "blog"
        //Vung cua chuoi str1: tu vi tri 14
        //Vung cua chuoi str2: tu vi tri 3, co 4 ki tu ("blog")
        //tra ve true or false
        System.out.print(str1.regionMatches(14, str2, 3, 4));

        //SO SANH THOI GIAN THUC THI CAU LENH

        long startTime = System.currentTimeMillis();
      for(int i=0;i<50000;i++){
         String strF = "hello";
         String strL = "hello"; 
      }
      long endTime = System.currentTimeMillis();
      System.out.println("Time taken for creation" 
      + " of String literals : "+ (endTime - startTime) 
      + " milli seconds" );       
      long startTime1 = System.currentTimeMillis();
      for(int i=0;i<50000;i++){
         String strF1 = new String("hello");
         String strL1 = new String("hello");
      }
      long endTime1 = System.currentTimeMillis();
      System.out.println("Time taken for creation" 
      + " of String objects : " + (endTime1 - startTime1)
      + " milli seconds");
        

    //FORMAT()

    double e = 2.25874;
    System.out.format(Locale.GERMANY,"%1.4f%n%n", e);
    

        //LAY KI TU UNICODE

    System.out.print(s1.codePointAt(2));

    }
   
    
    public static String removeCharAt(String s, int i)
    {
        return s = s.substring(0,i) + s.substring(i+1);
    }
    
}






Chúc các bạn học tốt!
Góp ý tại mục bình luận bên dưới 💓