c#如何分隔3个特殊字符尀r尀n和尀0

2025-01-01 05:36:40
推荐回答(1个)
回答1:

String[] lines = { "abc\ndef\r\nghi", "abc\n\n\ndef\r\n\r\n\r\nghi", "abc\n\n\r\n\n\r\n\ndef\r\n\n\n\r\n\r\nghi" }; for (int k = 0; k < lines.length; k++) { String[] arr = Pattern.compile("[\r\n]+|[\n\r]+|[\n]+").split( lines[k]); System.out.println(); for (int i = 0; i < arr.length; i++) { System.out.println(i + "[" + arr[i] + "]"); } }Console:0[abc]1[def]2[ghi]0[abc]1[def]2[ghi]0[abc]1[def]2[ghi]建议楼主先将'\r'转化为'\n'再进行splite操作,即如下:String[] arr = Pattern.compile("[\n]+").split(lines[k].replace('\r', '\n'));