zoukankan      html  css  js  c++  java
  • HW—指定字符替换Java(replace)---动态和静态定义字符串数组;

    总结:

    1. 语法层面上:这里主要用到Java字符串的替换函数,str.replaceAll("待替换的","替换成的")。replaceAll接受的是正则花的regex
    2. 还要注意替换不影响原来的字符串,只有左边付给原来的字符串时,才达到彻底替换的结果。你也可以定义一个新的字符串去保存替换后的结果;
    3. 对于这种指定格式的输入,只要考虑按照标准输入就行了,而且按照这种标准输入的话,还要对相关字符串进行提取,比如用正则化提取的“->”;
    4. 定义字符串数组:  

      (1)静态的方法:   
      String tmp[]=new String[2]; 
      (2)动态的方法: ArrayList<String> strArray = new ArrayList<String> (); 比较灵活 或者Vector<String> vec=new Vector<String>;
    5. 最后算法上没有什么值得深究的地方;

    题目要求:输入一个字符串,然后在输入一个整数,就是替换字符串的次数,然后依次输入需要替换的字符串……

    例如:

    输入:asdfghjasdfghj

               3

               as->bnm

               df->qwe

               gh->yui

    输出:bnmqweyuijbnmqweyuij

    意思就是,将输入的字符串中,as替换成bnm,df替换成qwe,gh替换成yui,总共替换三次,注意次数是不限定的,可以是任意整数等。

    如果输入的次数是2,举例说明:

    输入:asdfgasdfg

               2

               as->bn

               df->yuio

    输出:bnyuiogbnyuiog

    电脑测试版本:

    import java.util.*;
    public class StringReplace {
        public static void main(String[] args){
            System.out.println("请输入字符串:");
            Scanner strOr=new Scanner(System.in);
            String strIn = strOr.nextLine();        
            
            System.out.println("请输入要替换的个数:");
            Scanner intOr=new Scanner(System.in);
            int count= intOr.nextInt();        
            
            String tmp[]=new String[2];       //分隔
            String str[]=new String[count];  //保存
            for(int i=0;i<count;i++){
                Scanner strOr2=new Scanner(System.in);
                str[i]=strOr2.nextLine();
            }
            
            for(int j=0;j<count;j++){
                tmp=str[j].split("->");   //分隔存放到tmp中,这里是假设知道分隔后的长度
                strIn=strIn.replaceAll(tmp[0], tmp[1]);
            }
            System.out.println(strIn);
        }
    
    }

    提交版本:

    import java.util.*;
    public class StringReplace {
        public static void main(String[] args){
           // System.out.println("请输入字符串:");
            Scanner strOr=new Scanner(System.in);
            String strIn = strOr.nextLine();        
            
          //  System.out.println("请输入要替换的个数:");
            Scanner intOr=new Scanner(System.in);
            int count= intOr.nextInt();        
            
            String tmp[]=new String[2];       //分隔
            String str[]=new String[count];  //保存
            for(int i=0;i<count;i++){
                Scanner strOr2=new Scanner(System.in);
                str[i]=strOr2.nextLine();
            }
            
            for(int j=0;j<count;j++){
                tmp=str[j].split("->");   //分隔存放到tmp中,这里是假设知道分隔后的长度
                strIn=strIn.replaceAll(tmp[0], tmp[1]);
            }
            System.out.println(strIn);
        }
    
    }
  • 相关阅读:
    ISO、光圈、曝光、焦距
    错误: Could not edit the data in the folder or database selected. The current version does not support editing (base, consistent, or closed)
    SVN无法读取cruuent修复方法 Can't read file : End of file found 文件:txn_current、current
    visio 2010 修改 默认字体 字号大小 方法
    IE9最大化失效,最大化后顶端依然是桌面
    解决Excel导入Datatable 255字符限制(oledbdataadapter原理)
    关闭“子由门”FreeGate 无法 无线上网 有线网络可以上网
    进程死锁及解决办法
    纯css实现元素下出现横线动画(backgroundimage)
    html area标签详解
  • 原文地址:https://www.cnblogs.com/snowwhite/p/4736703.html
Copyright © 2011-2022 走看看