text = Daily 10 am - 5 pm.\\nClosed Thanksgiving and Christmas.
private String activateNewlines( String text ) {
String temp = text;
if ( text.contains( "\\n") ) {
while ( temp.contains( "\\n" ) ) {
int index = temp.indexOf( "\\n" );
temp = temp.substring( 0, index ) + temp.substring( index + 1 );
}
return temp;
}
return text;
}
我试图摆脱特殊字符的额外斜杠,但由于某种原因,子字符串最终删除了正斜杠。子字符串不喜欢字符串开头的斜线吗?最后的字符串最终变成
Daily 10 am - 5 pm.nClosed Thanksgiving and Christmas.
我需要的是
Daily 10 am - 5 pm.\nClosed Thanksgiving and Christmas.
编辑:什么最终为我工作:
String temp = text;
if ( text.contains( "\\n") ) {
temp = temp.replaceAll( "\\\\n", "\\\n" );
int x = 5;
return temp;
}
return text;
这实际上允许 TextView 识别换行符。