题解 | 字符串操作-Java

字符串操作

https://www.nowcoder.com/practice/06a5336b64e3481fbbcc1f7d5cba548d?tpId=383&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int length = in.nextInt();
        int round = in.nextInt();
        String str = in.next();
        for (int i = 1; i <= round; i++) {
            int start = in.nextInt() - 1;
            int end = in.nextInt() - 1;
            String oldLetter = in.next();
            String newLetter = in.next();

            str = str.substring(0, start) + str.substring(start, end + 1).replace(oldLetter,
                    newLetter) + str.substring(end + 1);
        }
        System.out.print(str);

    }
}

全部评论

相关推荐

评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务