我正在尝试创建此列表的字符串而不使用以下字符, []
,因为我想在删除它们之后替换所有两个空格。
我已尝试过以下内容,但我正在考虑标题中的错误。 的简单:
[06:15, 06:45, 07:16, 07:46]
结果应如下所示:
06:15 06:45 07:16 07:46
代码:
List<String> value = entry.getValue();
String timeEntries = value.toString();
String after = timeEntries.replaceAll(",", " ");
String after2 = after.replaceAll(" ", " ");
String after3 = after2.replaceAll("[", "");
String after4 = after3.replaceAll("]", "");
答案 0 :(得分:2)
replaceAll
会替换与给定正则表达式匹配的所有匹配项。由于您只想匹配一个简单的字符串,因此您应该使用replace
代替:
void OnGUI(){
if (visible) {
GUI.skin = skin;
GUI.Window(0,new Rect((Screen.width-1024)/2,0,1024,600),InventoryBody,"Inventory");
}
}
void InventoryBody(int id){
GUIStyle style = new GUIStyle();
if (currentItem) {
GUI.DrawTexture (new Rect(550f,70f,80f,80f), currentItem.texture);
GUI.color = Color.red;
GUI.Label (new Rect(700f,50f,400f,300f), currentItem.name);
GUI.color = Color.black;
string desc = "Description: "+currentItem.description;
style.wordWrap=true;
style.fontSize=18;
GUI.Label (new Rect(650f,100f,300f,500f), desc,style);
if(GUI.Button(new Rect(700f, 290f, 150f,50f), "Cancel")) {
currentItem = null;
}
if(GUI.Button(new Rect(700f, 230f, 150f,50f), "Use")) {
currentItem.Use ();
}
}
//1st column
GUILayout.BeginArea (new Rect (80f,60f,600f,600f));
for (int i = 0; i<5; i++) {
if(items[i]!=null){
if(GUILayout.Button (items [i].texture, GUILayout.Width (80f), GUILayout.Height (80f)) ){
currentItem = items[i];
}
} else {
GUILayout.Box("", GUILayout.Width(80f),GUILayout.Height(80f));
}
}
GUILayout.EndArea ();
//2nd column
GUILayout.BeginAr...
}
答案 1 :(得分:1)
要回答主要问题,如果您使用replaceAll
,请确保您的第一个参数是有效的正则表达式。对于您的示例,您实际上可以将其减少为2次调用replaceAll
,因为其中2次替换相同。
List<String> value = entry.getValue();
String timeEntries = value.toString();
String after = timeEntries.replaceAll("[, ]", " ");
String after2 = after.replaceAll("\\[|\\]", "");
但是,看起来你只是试图将String列表的所有元素连接在一起。通过迭代列表并使用StringBuilder:
,直接构造此字符串会更有效StringBuilder builder = new StringBuilder();
for (String timeEntry: entry.getValue()) {
builder.append(timeEntry);
}
String after = builder.toString();