java - Remove string after second colon in Java
问题描述
I would like to remove the string after the second colon and add a new string behind.
I have a maker when clicking on a marker I add the string using ALertDialog on the marker title so I can get those value to the process for a text file. But if I select that marker again I want to remove that text after colon so I don't add duplicate string again.
I checked this link but it uses a regular expression and I don't have any experience with that.
My String = W:3:ER1,ER2,ER3,,,,
Output = W:3
I would like to remove the string after the second colon so I can attach my string
Here is my onMarkerClick
@Override
public boolean onMarkerClick(final Marker marker) {
final String[] title = {marker.getTitle()};
Bitmap smallDot = Bitmap.createScaledBitmap(getBitmapFromVectorDrawable(getApplicationContext(),
R.drawable.blue_dot), 15, 15, false);
final String[] selectedItems = {"", "", "", "", ""};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Enable Relays");
// add a checkbox list
final String[] relays = {"Relay 1", "Relay 2", "Relay 3", "Relay 4", "Implement"};
boolean[] checkedItems = {true, false, false, true, false};
builder.setMultiChoiceItems(relays, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
selectedItems[which] = "ER" + (which + 1);
} else if (!isChecked) {
// Else, if the item is already in the array, remove it
selectedItems[which] = "";
}
Toast.makeText(MainActivity.this, "" + Arrays.toString(selectedItems), Toast.LENGTH_LONG).show();
}
}).setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// title[0] += ":" + selectedItems[0] + "," + selectedItems[1] + "," + selectedItems[2] + "," + selectedItems[3] + "," + selectedItems[4];
int commas = 0;
for (int i = 0; i < title[0].length(); i++) {
if (title[0].charAt(i) == ',') commas++;
}
if (!title[0].contains("ER1") && commas <= 4)
title[0] += ":" + selectedItems[0];
if (!title[0].contains("ER2") && commas <= 4)
title[0] += "," + selectedItems[1];
if (!title[0].contains("ER3") && commas <= 4)
title[0] += "," + selectedItems[2];
if (!title[0].contains("ER4") && commas <= 4)
title[0] += "," + selectedItems[3];
if (!title[0].contains("ER5") && commas <= 4)
title[0] += "," + selectedItems[4];
marker.setTitle(title[0]);
marker.showInfoWindow();
Toast.makeText(MainActivity.this, "" + title[0], Toast.LENGTH_LONG).show();
}
}).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
return true;
}
解决方案
First we split on the last :
to get the title.
Then we parse on ,
from the last :
to get items and we filter out empty ones.
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
String raw = "W:3:ER1,ER2,ER3,,,,";
String title = raw.substring(0, raw.lastIndexOf(":"));
String[] selectedItems = raw.substring(raw.lastIndexOf(":")+1).split(",");
List<String> items = Arrays.asList(selectedItems).stream().filter(str ->
! "".equals(str)
).collect(Collectors.toList());
System.out.println("Title "+ title);
System.out.println("Items "+ items);
}
}
Prints
Title W:3
Items [ER1, ER2, ER3]
Edit 1 : Should handle single colon
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
String raw = "W:3";
List<String> items;
String title;
if (raw.replaceAll("[^:]", "").length() > 1) { // String with every :
title = raw.substring(0, raw.lastIndexOf(":"));
String[] selectedItems = raw.substring(raw.lastIndexOf(":") + 1).split(",");
items = Arrays.asList(selectedItems).stream().filter(str ->
!"".equals(str)
).collect(Collectors.toList());
} else {
title = raw;
items = null;
}
System.out.println("Title "+ title);
System.out.println("Items "+ items);
}
}
And prints for raw = "W:3";
Title W:3
Items null
推荐阅读
- php - 在 laravel 中处理 ajax 请求时出现问题 - 状态 400
- sas - 从文件夹导入 dbfs 时增加列名限制
- angular - 向 Go API 提交 Angular 表单不断发布 Nil
- jupyter-notebook - 在 google colab 中按顺序运行代码单元
- docker - Docker 登录 kube 集群
- search - NetSuite 已保存搜索:如何创建某个个人创建的平均每日交易数?
- sql - 使用已获得奖励的查询返回值
- git - .gitignore 不起作用 - 仍将文件夹显示为未跟踪
- swift - 服务器没有响应:未知:transportError
- angular - 错误类型错误:无法读取未定义的属性“id”(角度)