how to make thumbnail image with initials two char from name android?

前端 未结 4 620
粉色の甜心
粉色の甜心 2021-01-06 04:14

I want to thumbnail initials with two word for my image view like \"Peter Parker\" but am able to get only one word \"P\"while running code how can get second word after spa

4条回答
  •  花落未央
    2021-01-06 05:00

    You can do it functional way:

    val peterParker = "Peter Parker"
    
    val initials = peterParker
            .split(' ')
            .mapNotNull { it.firstOrNull()?.toString() }                     
            .reduce { acc, s -> acc + s }
    
    println(initials) //PP
    

    This would cover cases when a person's name consists of more than 2 words.

提交回复
热议问题