String 截断文本以在Scala中获得预览

String 截断文本以在Scala中获得预览,string,scala,String,Scala,我需要截断文本以获得预览。预览是~ n>代码>字符(但不多)的文本前缀,不应在中间拆分单词。 preview("aaa", 10) = "aaa" preview("a b c", 10) = "a b c" preview("aaa bbb", 5) = "aaa" preview("a b ccc", 3) = "a b" 预览(“aaa”,10)=“aaa” 预览(“ABC”,10)=“ABC” 预览(“aaa bbb”,5)=“aaa” 预览(“a b ccc”,3)=“a b” 我将函

我需要截断文本以获得预览。预览是~<代码> n>代码>字符(但不多)的文本前缀,不应在中间拆分单词。

preview("aaa", 10) = "aaa" preview("a b c", 10) = "a b c" preview("aaa bbb", 5) = "aaa" preview("a b ccc", 3) = "a b" 预览(“aaa”,10)=“aaa” 预览(“ABC”,10)=“ABC” 预览(“aaa bbb”,5)=“aaa” 预览(“a b ccc”,3)=“a b” 我将函数编码如下:

def preview(s:String, n:Int) = if (s.length <= n) s else s.take(s.lastIndexOf(' ', n)) def预览(s:String,n:Int)= 如果(s.length我建议下一个:

--更新--

def ellipsize(text : String, max : Int): String = {
  def ellipsize0(s : String): String =
    if(s.length <= max) s
    else {
      val end = s.lastIndexOf(" ")
      if(end == -1) s.take(max)
      else ellipsize0(s.take(end))
    }
  ellipsize0("\\s+".r.replaceAllIn(text, " "))
} 
def ellipsize(text:String,max:Int):String={
def ellipsize0(s:String):String=
如果(s.length)如果(s.length这个怎么样

def preview(s:String, n:Int) =
  if (s.length <= n) s 
  else s.take(n).takeWhile(_ != ' ')
def预览(s:String,n:Int)=

如果(s.length那么以下各项如何:

  def preview(s: String, n: Int) = if (s.length <= n) {
    s
  } else {
    s.take(s.lastIndexWhere(_.isSpaceChar, n + 1)).trim
  }
def预览(s:String,n:Int)=如果(s.length这似乎有效:

  // find the last word that is not split by n, then take to its end
  def preview(text: String, n: Int): String = 
    text take (("""\S+""".r findAllMatchIn text takeWhile (_.end <= n)).toList match {
      case Nil => n
      case ms  => ms.last.end
    })

您希望它对
预览(“aaaaaabbbbbc”,7)
预览(“aaaaa”,3)
有什么作用?
预览(“aaaaaabbbbbc”,7)
应该返回
“aaaaaaa”
预览(“aaaaa”,3)
“aaa”
您在
预览(“abccc”,3)=“abc>”上测试过吗?
  // find the last word that is not split by n, then take to its end
  def preview(text: String, n: Int): String = 
    text take (("""\S+""".r findAllMatchIn text takeWhile (_.end <= n)).toList match {
      case Nil => n
      case ms  => ms.last.end
    })
text take (("""\S+""".r findAllMatchIn text takeWhile (m => m.start == 0 || m.end <= n)).toList.last.end min n)
object Previewer {
  implicit class `string preview`(val text: String) extends AnyVal {
    // find the last word that is not split by n, then take to its end
    def preview(n: Int): String =
      text take (("""\S+""".r findAllMatchIn text takeWhile (_.end <= n)).toList match {
        case Nil => n
        case ms  => ms.last.end
      })
  }
}
class PreviewTest {
  import Previewer._

  @Test def shorter(): Unit = {
    assertEquals("aaa", "aaa" preview 10)
  } 
  @Test def spacey(): Unit = {
    assertEquals("a b c", "a b c" preview 10)
  }
  @Test def split(): Unit = { 
    assertEquals("abc", "abc cba" preview 5)
  }   
  @Test def onspace(): Unit = {
    assertEquals("a b", "a b cde" preview 3)
  } 
  @Test def trimming(): Unit = {
    assertEquals("a b", "a b    cde" preview 5)
  } 
  @Test def none(): Unit = {
    assertEquals(" " * 5, " " * 8 preview 5)
  }
  @Test def prefix(): Unit = {
    assertEquals("a" * 5, "a" * 10 preview 5)
  } 
}