java - java不能连接到字符串的结尾
问题描述
我正在尝试将“.png”连接到字符串变量“text”的末尾。然而,我尝试的每种方法最终都会用“.png”替换整个字符串。
Scanner sc = new Scanner(new File("C:/Users/censored/file.csv"));
//file has "test,test1,test2,test3" from an excel file
sc.useDelimiter(",");
sc.useDelimiter("\n");
while (sc.hasNext()) {
String text = sc.next();
String pathname = "C:/Users/censored/" + text;
System.out.println(pathname); //prints "C:/Users/censored/test"
pathname += ".png";
System.out.println(pathname); //prints ".png" only
}
我觉得它必须与scanner/sc.next()有关,因为如果我将文本定义为字符串文字,我可以获得正确的结果。我能做些什么来解决这个问题?
解决方案
您使用该useDelimiter()
方法的方式,仅使用“\n”
尝试这个:
sc.useDelimiter(",|\\n");
推荐阅读
- python - 为什么这个字典值都是一样的?
- qt - 是否可以在 QML 地图上创建 3D 折线?
- react-native - 需要创建一个显示数组列表的设计
- swift - SpriteKit 场景的大小/缩放不正确
- azure - 排除 Azure CI 管道 (YAML) 中的某些文件
- google-sheets - 如何检查 IMAGE 公式中使用的 url 是否正确并显示图像?
- c# - 正则表达式拆分换行符 - 在匹配结束时保留捕获的换行符
- php - 以特定方式显示文件夹中的图片 - PHP
- java - 测试在本地通过,但在 git 管道中失败
- javascript - 在 React 上单击父元素时如何触发子元素?