我有以下字符串。
#0
lock;
faceplayer;
say Hello Player!;
end;
#1
lock;
say I don't want to talk to you.;
end;
我想传递一个int并且让方法返回一个从#int开始的字符串,直到结束为止; 怎么办呢?
答案 0 :(得分:0)
尝试这样的事情:
#0.to_s
lock;
faceplayer;
say Hello Player!;
end;
#1.to_s
lock;
say I don't want to talk to you.;
end;
或者
"#0";
lock;
faceplayer;
say Hello Player!;
end;
"#1";
lock;
say I don't want to talk to you.;
end;
答案 1 :(得分:0)
取出那个字符串并拆分'#'然后你会得到一个数组
前:
String[] split = yourString.split("#");
结果:
0 锁; faceplayer; 说你好玩家! 端;
1 锁; 说我不想跟你说话。 端;
将你的#添加回到它的开头,你就可以了。
答案 2 :(得分:0)
我想你想要这样的东西。使其具有相当的参数化灵活性。
public static String parse(int from, int to, String str) {
if(str.indexOf("#" + Integer.toString(to)) > 0) {
return str.substring(str.indexOf("#" + Integer.toString(from)), str.indexOf("#" + Integer.toString(to)));
} else {
return str.substring(str.indexOf("#" + Integer.toString(from)));
}
}
答案 3 :(得分:0)
不是将文本放入字符串,而是放入文件中。这样,每次更改文本时都不必重新编译代码。我知道这不是您可能已经考虑/解释过的确切解决方案,但它是一个可以满足您的要求/需求的解决方案。希望这会对你有所帮助。
public class YourClass {
public static void main(String[] args) {
int tagId = 1;
System.out.println(searchPrint(tagId));
}
private static StringBuilder searchPrint(int tagId) {
String tag = Integer.toString(tagId);
StringBuilder result = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader("/tmp/one.txt"));
try {
String line = br.readLine();
boolean display = false;
// loop through to read each line in the file
while (line != null) {
// set the flag to true so then we can print as soon as you
// find it
if (line.contains("#" + tag)) {
display = true;
}
// set the flag to false to stop print as soon as the end
// tag is found
else if (line.contains("end;")) {
display = false;
}
if (display == true) {
result.append(line);
//System.out.println(line);
}
line = br.readLine();
}
} finally {
br.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}