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);
        }
    
    }
  • 相关阅读:
    vb 使用Cystal Reports 9小例子
    VB Export sample
    c++文件結束符
    初学PHP:用post传递checkbox
    VB 图片在数据库的导入与导出
    vb 事务sample
    linux查找进程并杀掉进程
    WebRequest 对象的使用
    Asp操作Cookies(设置[赋值]、读取、删除[设置过期时间])
    .net webrequest应用
  • 原文地址:https://www.cnblogs.com/snowwhite/p/4736703.html
Copyright © 2011-2022 走看看