LOADING

Type to search

Coding Java

Extracting desired values from a String

Share

Extracting desired values from a string to confirm would be required during various occasions of your automation. There are various approaches to achieve this yet which is the better way. Goodness is it better way, how might you say that? this would be.

You have to have a superior method of actualizing code which set aside lesser time to execute, which is the primary reason for automation to lessen the timetables for your test execution.

For instance:

You have to extricate the name from the email address. To accomplish this undertaking we do have numerous ways.

However we would be looking into just not many which are generally utilized.

Choice 1:

You can use the split method to separate email address to 2 strings to String[] and print String[0].

public static void main (String[] args){
       String str = "[email protected]";
       String[] strs = str.split("@");
       System.out.println(strs[0]);
}
Choice 2:

By using Pattern and Collectors methods you can achieve the task, this would be working only using Java 9 or above.

import java.util.regex.Pattern;
import java.util.stream.Collectors;

public static void main (String[] args){ 
       String str = "[email protected]"; 
       str = Pattern.compile("^([^@]+)") 
                    .matcher(str).results() 
                    .map(result ->; result.group(1)) 
                    .collect(Collectors.joining(" ")); 
       System.out.println(str); 
}
Choice 3:

By using replaceAll(); method.

public static void main (String[] args){
       String str = "[email protected]";
       System.out.println(str.replaceAll("@.+$", "").trim());
}
Choice 4:

By using subString() method.

public static void main (String[] args){
       String str = "[email protected]";
       System.out.println(str.substring(0,str.lastIndexOf("@")));
}

Above were the most utilized approaches to separate wanted username from the email address, Now we would investigate which technique is better fits with less execution time.

For breaking down the time slipped by to execute particular strategies, we utilized underneath code.

import java.util.concurrent.TimeUnit;

long start = System.nanoTime(); 
long end = System.nanoTime(); 
long elapsed = TimeUnit.MILLISECONDS.toMillis(end-start);

For Example:

public static void main (String[] args){ 
       String str = "[email protected]"; 
       long start = System.nanoTime(); 

       String[] strs = str.split("@"); 

       long end = System.nanoTime(); 
       long elapsed = TimeUnit.MILLISECONDS.toMillis(end-start); 
       System.out.println(strs[0] + "Elapsed time: " + elapsed); 
}
MethodElapsed Time (Nano Sec)
Choice 1631400
Choice 251746599
Choice 320600
Choice 414300

As per above results Choice 4 took least and Choice 2 took max time to execute. Now you can decide which method you want to adopt for Extracting desired values from a String.

Definitely it should be choice 4 which I would be using, what about you?: Wikipedia

Leave a Comment

Your email address will not be published. Required fields are marked *