简体   繁体   中英

Java preferred line break in a String

I have a String like this:

"speed1: 10 km/h, speed2: 20 km/h, speed3: 30 km/h, speed4: 40 km/h, speed5: 50 km/h"

I want to display this string in a TextView, which can have a variable width. On some devices, the hole String fits in one line. On other devices, 2-3 lines are needed.

I want the String to be broken only at the comma- characters, so that parameters and values are on the same line.

Is there a special character in Android for that? (Or do I have to first let the String be drawn, count the number of lines, and then replace some commas by a newline character).

Maybe ugliest solution in world, but how about replacing the "normal" spaces except the ones after comma, with Unicode non-breaking space? ("\ ")?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM