最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java探索之string字符串的應(yīng)用代碼示例

 更新時間:2017年10月27日 08:41:13   作者:書思BookReflect  
這篇文章主要介紹了Java探索之string字符串的應(yīng)用代碼示例,具有一定參考價值,需要的朋友可以了解下。

String類中提供了豐富的用于操作字符串的方法。

int indexOf(String str)

該方法用于返回當(dāng)給定字符串在當(dāng)前字符串中的位置,若當(dāng)前字符串不包含給定字符串則返回-1。

重載的方法

int indexOf(String str,int formIndex),從指定下標(biāo)處(包含)查詢子串,返回返回當(dāng)給定字符串在當(dāng)前字符串中的位置,若當(dāng)前字符串不包含給定字符串則返回-1。

用自己的算法實現(xiàn)startsWith和endsWith功能。

package com.hz.practice;
/**
 * 1. 用自己的算法實現(xiàn)startsWith和endsWith功能。
 * @author ztw
 *
 */
public class Practice01 {
  public static void main(String[] args) {
    String str = "qwewrewr";
//   boolean temp = str.endsWith("r");
//   System.out.println(temp);
    /*
     * startsWith
     */
    char[] arr = str.toCharArray();
    char c = 'r';
    if(arr[0]==c){
      System.out.println("true");
    }else{
      System.out.println("false");
    }
    /*
     * endsWith
     */
    if(arr[arr.length-1]==c){
      System.out.println("true");
    }else{
      System.out.println("false");
    }
  }
}

輸出結(jié)果:

false 
true

2.采用字符的移位方式實現(xiàn)字符文本加密解密。

package com.hz.practice;
import java.util.Scanner;
/**
 * 2.采用字符的移位方式實現(xiàn)字符文本加密解密。
 * @author ztw
 *
 */
public class Practice02 {
  public static void main(String[] args) {
    System.out.println("請輸入一個字符串");
    Scanner sc =new Scanner(System.in);
    String str1 = new String();
    str1=sc.nextLine();    
    System.out.println(str1.replaceAll("a", "1.")
        .replaceAll("b", "2.").replaceAll("c", "3.")
        .replaceAll("d", "4.").replaceAll("e", "5.")
        .replaceAll("f", "6.").replaceAll("g", "7.")
        .replaceAll("h", "8.").replaceAll("i", "9.")
        .replaceAll("j", "10.").replaceAll("k", "11.")
        .replaceAll("l", "12.").replaceAll("m", "13.")
        .replaceAll("n", "14.").replaceAll("o", "15.")
        .replaceAll("p", "16.").replaceAll("q", "17.")
        .replaceAll("r", "18.").replaceAll("s", "19.")
        .replaceAll("t", "20.").replaceAll("u", "21.")
        .replaceAll("v", "22.").replaceAll("w", "23.")
        .replaceAll("x", "24.").replaceAll("y", "25.")
        .replaceAll("z", "26."));
  }
}

輸出結(jié)果:

請輸入一個字符串 
asdsddffg 
1.19.4.19.4.4.6.6.7.

3.驗證是隨機(jī)生成4位驗證碼,由用戶輸入并否輸入正確, 如果輸入錯誤就生成新的驗證碼讓用戶重新輸入,最多輸入5次!

package com.hz.practice;
import java.util.Random;
import java.util.Scanner;
/**
 * 3.驗證是隨機(jī)生成4位驗證碼,由用戶輸入并否輸入正確,
 * 如果輸入錯誤就生成新的驗證碼讓用戶重新輸入,最多輸入5次
 * @author ztw
 *
 */
public class Practice03 {
  public static void main(String[] args) {
    String str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char[]arr=new char[4];//定義一個長度是4的char型數(shù)組
    Random sj=new Random();
    System.out.println("驗證碼是:");
    for(int i=0;i<4;i++)
    {
      arr[i]=str.charAt(sj.nextInt(61));//從str中隨機(jī)截取4個單個字符并賦值給arr這個數(shù)組存放
    }
    System.out.println(arr);
    Scanner sc=new Scanner(System.in);
    System.out.println("請輸入驗證碼");
    String a=new String(arr);//把數(shù)組轉(zhuǎn)換成字符串
    //定義輸入次數(shù)
    for(int j=0;j<5;j++)
    {
      if(sc.nextLine().equals(a))
      {
        System.out.println("驗證碼輸入正確");
      }
      else
      {
        System.out.println("驗證碼輸入有誤,請重新輸入");
        if(j<=3)
        {
          System.out.print("請輸入驗證碼");
          for(int i=0;i<4;i++)
          {
            arr[i]=str.charAt(sj.nextInt(61));//從str中隨機(jī)截取4個單個字符并賦值給arr這個數(shù)組存放
          }
          System.out.println(arr);
          a=new String (arr);
        }
        else
        {
          System.out.println("輸入有誤,對不起,5次機(jī)會已用完");
        }
      }
    }
  }
}

輸出結(jié)果:

驗證碼是: 
AVA8 
請輸入驗證碼 
AVA8 
驗證碼輸入正確
package com.hz.practice;

/**
 * 4.獲取一個字符串在另一個字符串中出現(xiàn)的次數(shù)
  思路:
  1.定義一個計數(shù)器。
  2.獲取子串第一次出現(xiàn)的位置
  3.從第一次出現(xiàn)的位置后剩余的字符串中繼續(xù)獲取子串出現(xiàn)的位置,每獲取一次計數(shù)器加1
  4,當(dāng)獲取不到時,計數(shù)完成
 * @author ztw
 *
 */
public class Practice04 {

  public static void main(String[] args) {

    String str1 = "asdasdas";
    String str2 = "as";
    //1.定義一個計數(shù)器。
    int total = 0;
    for (String temp = str1; temp!=null && 
        temp.length()>=str2.length();) {
      //2.獲取子串第一次出現(xiàn)的位置
      if(temp.indexOf(str2) == 0){
        //3.從第一次出現(xiàn)的位置后剩余的字符串中繼續(xù)獲取子串出現(xiàn)的位置,每獲取一次計數(shù)器加1
        total ++;
      }
      temp = temp.substring(1);
      System.out.print(temp+", ");
    }
    //4,當(dāng)獲取不到時,計數(shù)完成
    System.out.println("計數(shù)完成:"+total);
  }
}

輸出結(jié)果:

sdasdas, dasdas, asdas, sdas, das, as, s, 計數(shù)完成:3
package com.hz.practice;
/**
 * 5.獲取兩個子串中最大相同子串
   思路:
   1.將短的哪個子串按照長度遞減的方式獲取到
   2.將每次獲取到的子串去長串中判斷是否包含,如果包含,已經(jīng)找到
 * @author ztw
 *
 */
public class Practice05 {
   public static String getMaxSubString(String s1,String s2) { 
      String max = "",min = ""; 
      max = (s1.length()>s2.length())?s1: s2; 
      min = (max==s1)?s2: s1;    
//     sop("max="+max+"...min="+min); 
      for(int x=0; x<min.length(); x++) 
      { 
        for(int y=0,z=min.length()-x; z!=min.length()+1; y++,z++) 
        { 
          String temp = min.substring(y,z);         
          sop(temp); 
          if(max.contains(temp))//if(s1.indexOf(temp)!=-1) 
            return temp; 
        } 
      } 
      return ""; 
    } 
    public static void main(String[] args)  
    { 
      String s1 = "ab"; 
      String s2 = "cvhellobnm"; 
      sop(getMaxSubString(s2,s1)); 
    } 
    public static void sop(String str) 
    { 
      System.out.print(str+", "); 
    } 
}

輸出結(jié)果:

ab, a, b, b,
package com.hz.practice;
import java.util.Scanner;
/**
 * 6、寫一個方法判斷一個字符串是否對稱
 * @author ztw
 *
 */
public class Practice06 {
  public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    String str = input.next();//接收任意字符串
    isOk1(str);            
}
  /**
   * 判斷字符串是否對稱的方法(一)
   * 通過取取索引對應(yīng)值來進(jìn)行一一比對
   * @param str
   */
  public static void isOk1(String str){
      boolean result = true;
      int count =(str.length()-1)/2;
      for (int x=0;x<=count;x++ ){
          if(str.charAt(x)!=str.charAt(str.length()-1-x)){
              result = false;
              break;
          }
      }
      if(!result)
          System.out.println("該字符串是不對稱的");
      else
          System.out.println("該字符串是對稱的");
  }
  /*
   * public static void main(String[] args){
        Scanner input = new Scanner(System.in);
      String str = input.next();//接收任意字符串
          if (isOk2(str)==true) {
            System.out.println("真,對稱!");
        }else{
            System.out.println("假,不對稱!");
        }
    }
    /**
     * 方法二
     * 通過String加強(qiáng)類中的取反方法reverse獲取其逆向值
     * 再與原字符串相比是否相等!
     * 等于則返回TRUE,否則FALSE
     * @param str
     * @return
     */
  /*
    public static boolean isOk2(String str){
        StringBuffer sb = new StringBuffer(str);
        String str2 = sb.reverse().toString();
        return str.equals(str2);
    }
   */
}

輸出結(jié)果:

asdsa 
該字符串是對稱的
package com.hz.practice;
/**
 * 
  9、編寫一個程序,將下面的一段文本中的各個單詞的字母順序翻轉(zhuǎn),
  “To be or not to be",將變成"oT eb ro ton ot eb."。
 * @author ztw
 *
 */
public class Practice09 {
  public static void main(String[] args) {
    String str = "To be or not to be";
    String[] str2 = str.split(" ");
    for (int i = 0; i < str2.length; i++) {
      char[] ci = str2[i].toCharArray();
      System.out.print(" ");
      for (int j = ci.length-1; j >= 0 ; j--) {
        System.out.print(ci[j]);
      }
    }
  }
}
package com.hz.practice;
/**
 * 10、已知字符串:"this is a test of java". 
  按要求執(zhí)行以下操作:
  (1) 統(tǒng)計該字符串中字母s出現(xiàn)的次數(shù)
  (2) 取出子字符串"test"
  (3) 用多種方式將本字符串復(fù)制到一個字符數(shù)組Char[] str中.
  (4) 將字符串中每個單詞的第一個字母變成大寫, 輸出到控制臺。
  (5) 用兩種方式實現(xiàn)該字符串的倒敘輸出。(用StringBuffer和for循環(huán)方式分別實現(xiàn))
  (6) 將本字符串轉(zhuǎn)換成一個字符串?dāng)?shù)組,要求每個數(shù)組元素都是一個有意義的額英文單詞,并輸出到控制臺
 * @author ztw
 *
 */
public class Practice10 {
  public static void main(String[] args) {
    String str = "this is a test of java";
//   (1) 統(tǒng)計該字符串中字母s出現(xiàn)的次數(shù)
    int count = 0;
    for (int i = 0; i < str.length(); i++) {
      if(str.charAt(i)=='s'){
        count++;
      }
    }
    System.out.println("字符串中字母s出現(xiàn)的次數(shù):"+count);
//   (2) 取出子字符串"test"
    String str2 = (String) str.subSequence(10, 14);
    System.out.println(str2);
//   (3) 用多種方式將本字符串復(fù)制到一個字符數(shù)組Char[] str中.
    char[] c = str.toCharArray();
    System.out.println(c);
    for (int i = 0; i < c.length; i++) {
      System.out.print(c[i]);
    }
    System.out.println();
//   (4) 將字符串中每個單詞的第一個字母變成大寫, 輸出到控制臺。
      str=str.toLowerCase();
      String[] tt=str.split(" ");
      System.out.println(tt.length);
      for(int i=0;i<tt.length;i++)
      {  
        //加個判斷,長度大于0的
        if(tt[i].length()>0){
          System.out.print(String.valueOf(tt[i].charAt(0)).toUpperCase());
          System.out.print(tt[i].substring(1)+" ");
        }
      }
      System.out.println();
//   (5) 用兩種方式實現(xiàn)該字符串的倒敘輸出。(用StringBuffer和for循環(huán)方式分別實現(xiàn))
      StringBuffer sb = new StringBuffer(str);
      System.out.println(sb.reverse().toString());
      char[] c3 = str.toCharArray();
      for (int i = c3.length-1; i >= 0 ; i--) {
        System.out.print(c3[i]);
      }
      System.out.println();
//   (6) 將本字符串轉(zhuǎn)換成一個字符串?dāng)?shù)組,要求每個數(shù)組元素都是一個有意義的額英文單詞,并輸出到控制臺
      String[] str5=str.split(" ");
      for (int i = 0; i < str5.length; i++) {
        System.out.print(str5[i]+", ");
      }
  }
}

輸出結(jié)果:

字符串中字母s出現(xiàn)的次數(shù):3 
test 
this is a test of java 
this is a test of java 
6 
This Is A Test Of Java 
avaj fo tset a si siht 
avaj fo tset a si siht 
this, is, a, test, of, java,

總結(jié)

以上就是本文關(guān)于Java探索之string字符串的應(yīng)用代碼示例的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:淺談Java編程ToString()方法重寫的意義java tostring方法重寫代碼示例等,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

相關(guān)文章

  • django 遞歸查詢評論的示例詳解

    django 遞歸查詢評論的示例詳解

    文章介紹了將表數(shù)據(jù)轉(zhuǎn)換為樹狀結(jié)構(gòu)以及如何處理扁平化數(shù)據(jù)的方法,探討了在數(shù)據(jù)管理中的應(yīng)用和實現(xiàn)技術(shù),感興趣的朋友一起看看吧
    2025-01-01
  • 通過實例解析Java List正確使用方法

    通過實例解析Java List正確使用方法

    這篇文章主要介紹了通過實例解析Java List正確使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11
  • Java中關(guān)于String StringBuffer StringBuilder特性深度解析

    Java中關(guān)于String StringBuffer StringBuilder特性深度解析

    這篇文章主要介紹了Java中關(guān)于String StringBuffer StringBuilder特性深度解析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • java實現(xiàn)日期拆分的方法

    java實現(xiàn)日期拆分的方法

    這篇文章主要介紹了java實現(xiàn)日期拆分的方法,基于java日期類實現(xiàn)對日期字符串的拆分功能,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • 動態(tài)修改spring?aop?切面信息提升自動日志輸出框架效率

    動態(tài)修改spring?aop?切面信息提升自動日志輸出框架效率

    這篇文章主要為大家介紹了動態(tài)修改spring?aop切面信息提升自動日志輸出框架效率,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • Spring實現(xiàn)聲明式事務(wù)的方法詳解

    Spring實現(xiàn)聲明式事務(wù)的方法詳解

    這篇文章主要介紹了Spring實現(xiàn)聲明式事務(wù)的方法詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • 解決線程并發(fā)redisson使用遇到的坑

    解決線程并發(fā)redisson使用遇到的坑

    這篇文章主要介紹了解決線程并發(fā)redisson使用遇到的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 微信公眾平臺開發(fā)實戰(zhàn)Java版之微信獲取用戶基本信息

    微信公眾平臺開發(fā)實戰(zhàn)Java版之微信獲取用戶基本信息

    這篇文章主要介紹了微信公眾平臺開發(fā)實戰(zhàn)Java版之微信獲取用戶基本信息 的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • 深入解析Java的設(shè)計模式編程中單例模式的使用

    深入解析Java的設(shè)計模式編程中單例模式的使用

    這篇文章主要介紹了深入解析Java的設(shè)計模式編程中單例模式的使用,一般來說將單例模式分為餓漢式單例和懶漢式單例,需要的朋友可以參考下
    2016-02-02
  • springboot2.x只需兩步快速整合log4j2的方法

    springboot2.x只需兩步快速整合log4j2的方法

    這篇文章主要介紹了springboot2.x只需兩步快速整合log4j2的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05

最新評論

高碑店市| 如东县| 民权县| 百色市| 长沙县| 田东县| 夏津县| 刚察县| 津南区| 延边| 偃师市| 深圳市| 曲水县| 库尔勒市| 汉寿县| 大英县| 金堂县| 将乐县| 永修县| 密山市| 封开县| 时尚| 诏安县| 鲁山县| 时尚| 阜阳市| 金塔县| 炎陵县| 建宁县| 西峡县| 理塘县| 繁峙县| 同心县| 滨州市| 庆云县| 芜湖市| 石楼县| 武鸣县| 上思县| 南岸区| 乌苏市|