英文:
Replacing " " with _ in AndroidStudio (Java)
问题
**问题:** 你好,我正在尝试使用以下代码在我的应用程序中替换字符串中的空格:
String nationality1 = dataSnapshot.child("ean_data").child("nationality1").getValue().toString().toLowerCase();
nationality1 = nationality1.replace(" ", "_");
String svgNationality = "flag_" + nationality1;
不幸的是,这不起作用。我看到了一些类似的问题,暗示该“ ”不是常规空格,而是其他一些类型的空格。
我认为以下方法可能有效,但有点绕:
String nationality1 = dataSnapshot.child("ean_data").child("nationality 1").getValue().toString().toLowerCase();
String[] parts = nationality1.split(" ");
String part1 = parts[0];
String part2 = parts[1];
String _nationality = part1 + "_" + part2;
在这个特定的例子中,字符串是“united states”,我想将其变为“united_states”。
**问题:** 有没有办法只使用.replace使这个方法起作用?数据是从Firebase中提取的,顺便提一下。
**解决方法:** 适用于包含多个空格的字符串的变通解决方法:
String nationality1 = "_" + dataSnapshot.child("ean_data")
.child("nationality 1").getValue().toString().toLowerCase();
if (nationality1.contains(" ")){
nationality1 = nationality1.replaceFirst("_", "");
String[] parts = nationality1.split(" ");
nationality1 = "";
for (int i = 0; i < parts.length; i++){
nationality1 = nationality1 + "_" + parts[i];
}
}
String svgNationality = "flag" + nationality1;
英文:
Problem: Hello, I am trying to replace a space within a string in my app using the following code:
String nationality1 = dataSnapshot.child("ean_data").child("nationality1").getValue().toString().toLowerCase();
nationality1 = nationality1.replace(" ", "_");
String svgNationality = "flag_" + nationality1;
Unfortunately, this doesn't work. I have seen a few similar questions that suggest that the
" " isn't a regular space but some other kind.
I think the following might work, but it's kind of a roundabout:
String nationality1 = dataSnapshot.child("ean_data").child("nationality 1").getValue().toString().toLowerCase();
String[] parts = nationality1.split(" ");
String part1 = parts[0];
String part2 = parts[1];
String _nationality = part1 + "_" + part2;
In this particular instance, the String is "united states" which I want to become "united_states".
Question: Is there any way to make this work with just .replace? The data is being drawn from firebase btw.
Workaround solution that encompasses strings with more than one space:
String nationality1 = "_" + dataSnapshot.child("ean_data")
.child("nationality 1").getValue().toString().toLowerCase();
if (nationality1.contains(" ")){
nationality1 = nationality1.replaceFirst("_", "");
String[] parts = nationality1.split(" ");
nationality1 = "";
for (int i = 0; i < parts.length; i++){
nationality1 = nationality1 + "_" + parts[i];
}
}
String svgNationality = "flag" + nationality1;
答案1
得分: 2
如果您的意图是将任何文本转换为snake_case(或lower_underscore),如果考虑到所有边界情况,这可能会变得非常棘手,因此使用一个库可能是最好的选择。
Java中一个有用的库可能是Google Guava的com.google.common.base.CaseFormat
。您可以在这里查看如何入门:https://github.com/google/guava
您可以查看CaseFormat.LOWER_UNDERSCORE方法。
有可能问题出在字符串使用了不同的空白字符,如果是这种情况,您应该使用String.replaceAll
方法结合RegEx来处理,但这不太可能。
您是否已经查看了替换方法生成的输出?您确定问题是下划线没有被替换,而不是SVG文件名的其他位置出现了问题?我猜这就是您想要实现的目标。
您的第二个选项只适用于国家名称恰好由两个单词组成的国家,但有很多国家的名称由多个单词组成(或只有一个单词)。此外,它还要求确切地使用了1个空格字符,而不是其他任何空白字符。
英文:
If your intent is to convert any text to snake_case (or lower_underscore), it can get quite tricky if you consider all edge cases, so using a library might be your best bet.
One useful library on Java for this could be Google Guava's com.google.common.base.CaseFormat
. You can see how to get started here: https://github.com/google/guava
You can look at the CaseFormat.LOWER_UNDERSCORE method.
It is possible the issue is with the string using a different whitespace character, in which case you should look into RegEx with the String.replaceAll
method, but this is unlikely.
Have you looked at the output the replace method is generating? Are you sure the issue is with the underscore not being replaced and not somewhere else on the svg file name? Which I assume is what you are trying to achieve.
Your second option would only work for countries with exactly 2 words in their name, but there are plenty of countries with more words than that (or single words). Also, it also requires that exactly 1 space character was used, and not any other whitespace characters.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论