gpt4 book ai didi

Java 无法替换后跟的字符串\n

转载 作者:行者123 更新时间:2023-12-01 06:03:07 28 4
gpt4 key购买 nike

我是一名大学生,正在从事一个学期的项目,但我的程序遇到了障碍。在我进一步讨论之前,请注意我查看了堆栈溢出上的类似线程,但它们似乎都不符合我的情况。

我有一个从 pdf 生成的字符串输入,其中包含表中的大量数据。问题是,由于格式原因,部门列的一些表条目从 1 行变为 2 行,我无法解决这个问题。例如,

PS 253(我的算法处理得很好)

MA

243HON(破坏一切)

我最终需要能够将它们放在同一行并删除 MA 之后的“\n”以将其发送到程序的其余部分。我尝试在部门代码 (MA) 之后检查\n 一两个索引位置,并更改从中获取 243HON 的索引,但这不起作用。

我也尝试过 String = string.replaceAll("MA\n", "MA ") 如代码中所示。删除 MA 和\n 之间的空格没有任何作用。这是我的代码的相关部分。谢谢!

public static String[] departments = {"\nAS","\nSF","\nAE","\nAF","\nAT","\nLAR","\nAMS","\nBIO","\nBA","\nCHM","\nLCH","\nCIV","\nCSO",
"\nCOM","\nCEC","\nCS","\nCYB","\nEC","\nEE","\nEGR","\nEP","\nES","\nFA","\nGCS","\nHS","\nHON","\nHF","\nHU","\nMA","\nME","\nWX",
"\nMSL","\nNSC","\nPE","\nPS","\nPSY","\nSIM","\nSS","\nSE","\nSP","\nSYS","\nUNIV","\nUA"};

public static String[] departmentsFix = {"\nAS \n","\nSF \n","\nAE \n","\nAF \n","\nAT \n","\nLAR \n","\nAMS \n","\nBIO \n","\nBA \n","\nCHM \n","\nLCH \n","\nCIV \n","\nCSO \n",
"\nCOM \n","\nCEC \n","\nCS \n","\nCYB \n","\nEC \n","\nEE \n","\nEGR \n","\nEP \n","\nES \n","\nFA \n","\nGCS \n","\nHS \n","\nHON \n","\nHF \n","\nHU \n","\nMA \n","\nME \n","\nWX \n",
"\nMSL \n","\nNSC \n","\nPE \n","\nPS \n","\nPSY \n","\nSIM \n","\nSS \n","\nSE \n","\nSP \n","\nSYS \n","\nUNIV \n","\nUA \n"};


public static void main(String[] args) {
// TODO Auto-generated method stub
Loader loader = new Loader();
try {
File file = new File("C:\\Users\\User\\Desktop\\EclipseWorkspace\\SE 300\\ER_SCHED_PRT.pdf");
PDDocument document = PDDocument.load(file);
PDFTextStripper s = new PDFTextStripper();
loader.content = s.getText(document);

String[] splitString = loader.content.split("Instructor", 2);

loader.content = splitString[1];

int index = 0;
for (String y : departmentsFix) {
//find any departments with a \n after them and replace it with a space
loader.content = loader.content.replaceAll(y, departments[index] + " ");
index++;
}

最佳答案

我刚刚修复了它。通过find函数,我发现格式不是\nMA\n,而是\nMA\r\n。改变这一点很大程度上解决了一个不相关的小错误的问题,可以通过使用额外的空间来补偿。尽管如此,还是感谢您的帮助。

关于Java 无法替换后跟的字符串\n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52461600/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com