我想在字符串中将所有主题标签设置为小写:
"你好我是一个男人 #Alelete 等我要做运动 #NeverGiveUp "
应该成为:
"你好我是一个男人 #athlete 等我要去 做运动 #nevergiveup "
答案 0 :(得分:1)
如果您的问题是如何将所有内容转换为简单字母希望这有帮助
String myString = "HashTag";
String myNewSimpleLetterString = myString.substring(0,5).toLowerCase(); //subString will get the characters from 0 - 5
答案 1 :(得分:0)
试用此代码
只需将字符串传递给此方法,此方法将返回格式化的字符串
public static String changeFormat(String beforeFormatStr) {
String[] arrStr = beforeFormatStr.split(" ");
String afterFormatStr = "";
int wordPos = 0;
for (String word : arrStr) {
String changeStr = word;
if (word.contains("#")) {
changeStr = word.toLowerCase();
}
if (wordPos == 0) {
afterFormatStr += changeStr;
} else {
afterFormatStr += " " + changeStr;
}
wordPos++;
}
return afterFormatStr;
}
答案 2 :(得分:0)
您可以使用正则表达式查找和替换小写标签。喜欢:
String line = "Hello I'm a men #Athlete et I'm going to do sport #NeverGiveUp";
String regEx = "\\S*#(?:\\[[^\\]]+\\]|\\S+)"; //Regular expression for matching hashtag
StringBuffer sb = new StringBuffer();
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(line);
while (m.find())
{
// Avoids throwing a NullPointerException in the case that you
// Don't have a replacement defined in the map for the match
String repString = m.group();
if (repString != null)
repString = repString.toLowerCase();
m.appendReplacement(sb, repString);
}
m.appendTail(sb);
String replacedHashtag = sb.toString();
答案 3 :(得分:0)
Hash不是字符串,因此您必须使用Regx matcher和Pattern来实现此目的。
List<String> obtained_hashwords = new ArrayList<>();
String text_data = "Hello I'm a men #Athlete et I'm going to do sport #NeverGiveUp and I want to format this string";
Pattern pattern = Pattern.compile("#\\w+");
Matcher matcher = pattern.matcher(text_data);
while (matcher.find())
{
obtained_hashwords.add(matcher.group());
Log.d("Hash word",matcher.group());
}
for (String obtained_hashword:obtained_hashwords)
{
String no_hash = obtained_hashword.replace("#","");
String lower_case_word = "#"+no_hash.toLowerCase();
text_data = text_data.replace(obtained_hashword,lower_case_word);
}
Log.d("changed line",text_data);