java实现将指定字符串替换为制定长度的空格

    科技2022-07-10  142

    package try1; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class answer { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt();// 组数 Queue<String> q = new LinkedList<>(); for (int i = 0; i < x; i++) { int a = sc.nextInt();// 每组的行数 String[] s = new String[a]; int b = sc.nextInt();// 替换成空格的个数 for (int j = 0; j < s.length; j++) { s[j] = sc.next(); } String[] n = dd(s, b); for (int j = 0; j < n.length; j++) { q.offer(n[j]); } } for (String string : q) { System.out.println(string); } } static String[] dd(String[] s, int b) { String a = "->";//这里是想要替换的字符串 String[] n = new String[s.length]; String m = new String(); for (int i = 0; i < b; i++) { m += " "; } for (int i = 0; i < s.length; i++) { if (s[i].contains(a)) { s[i] = s[i].replaceAll(a, m); } n[i] = s[i]; } return n; } }
    Processed: 0.031, SQL: 8