Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 space my code is.

  holder.imgName?.text=teamData[position].userImage.substring(0,1)
like image 877
Subhash Pandey Avatar asked Nov 21 '18 07:11

Subhash Pandey


1 Answers

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.

like image 71
Maroš Šeleng Avatar answered Oct 11 '22 13:10

Maroš Šeleng