Home > Enterprise >  Extract string elements from give list of strings in Java or Kotlin
Extract string elements from give list of strings in Java or Kotlin

Time:05-29

I have List of Strings. Ex: "Books.crime.code", "Books.crime.description", "Books.crime.notes", "Books.crime.status"

I need to extract strings as below. "code", "description", "notes", "status".

I am looking at Java/Kotlin program for the same.

CodePudding user response:

val list = listOf(
  "Books.crime.code",
  "Books.crime.description",
  "Books.crime.notes",
  "Books.crime.status"
)

val result = list.map { it.substringAfterLast(".") }

result.forEach(::println)

Output:

code
description
notes
status

CodePudding user response:

Two ways you could do this is with the String#split() method or with one of the overloaded String#substring() methods along with the String#lastIndexOf() method (as already mentioned in comments):

Using the String#split() method:

// List Interface of String to hold our desired strings:
List<String> list = Arrays.asList( "Books.crime.code", "Books.crime.description", 
                                   "Books.crime.notes", "Books.crime.status");

/* StringBuilder object for building our comma (,) 
   delimited string of retrieved substrings:    */
StringBuilder sb = new StringBuilder("");

/* Iterate through each string element within the list 
   using an enhanced `for` loop:               */
for (String strg : list) {

    /* Split the current string into an String[] array of 
       individual parts based on the period as delimiter: */
    String[] parts = strg.split("[.]");

    /* If the built string currently within the StringBuilder
       object is NOT Empty then append a comma to the string
       build.                  */
    if(!sb.toString().isEmpty()) {
        sb.append(", ");
    }
    
    /* Append the third part of the current string being 
       processed from the list into the StringBuilder 
       object.    */
    sb.append(parts[2]);
}

/* Display the String contained within the StringBuilder 
   object to the Console Window:                 */
System.out.println(sb.toString());

Console window will display: code, description, notes, status


Using the String#substring() method:

// List Interface of String to hold our desired strings:
List<String> list = Arrays.asList( "Books.crime.code", "Books.crime.description", 
                                   "Books.crime.notes", "Books.crime.status");

/* StringBuilder object for building our comma (,) 
   delimited string of retrieved substrings:    */
StringBuilder sb = new StringBuilder("");

/* Iterate through each string element within the list 
   using an enhanced `for` loop:               */
for (String strg : list) {

    // Retrieve the desired substring:
    String part = strg.substring(strg.lastIndexOf(".")   1);

    /* If the built string currently within the StringBuilder
       object is NOT Empty then append a comma to the string
       build.                  */
    if(!sb.toString().isEmpty()) {
        sb.append(", ");
    }

    /* Append the retrieved substring from the current 
       string being processed from the list into the 
       StringBuilder object.    */
    sb.append(part);
}

/* Display the String contained within the StringBuilder 
   object to the Console Window:                 */
System.out.println(sb.toString());

Console window will display: code, description, notes, status

CodePudding user response:

You can use a simple Kotlin inline function with map and filter for it:

fun extractString(strs: List<String>): List<String> = 
    strs.map { it.split(".").last() }.filter { it != "" }

Here is a link to a playground with a working example.

  • Related