StringHelpers.java [src/utils] Revision: default  Date:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Lucas Yaege
 */
public class StringHelpers {

    public static String[] splitWithQuotes( String text )
    {
        ArrayList<String> list = new ArrayList<>();
        Matcher m = Pattern.compile( "([^\"]\\S*|\".+?\")\\s*" ).matcher( text );

        while ( m.find() )
        {
            list.add( m.group( 1 ) ); // Add .replace("\"", "") to remove surrounding quotes.
        }

        return list.toArray( new String[0] );
    }

    public static String convertStringToDurationString( String text )
    {
        String durString = "PT";
        String[] parts = text.split( ":" );
        int hours = Integer.parseInt( parts[0] );
        int minutes = Integer.parseInt( parts[1] );

        if ( hours > 0 ) //If interval is at least one hour...
        {
            durString += hours + "H";
        }

        durString += minutes + "M";

        return durString;

    }

    public static boolean startsWithDigits( String text )
    {
        if ( text.isEmpty() )
        {
            return false;
        }

        String[] parts = splitWithQuotes( text );

        if ( parts.length > 0 )
        {
            return parts[0].matches( "\\d+" );
        }
        else
        {
            return false; //empty strings don't start with digits.
        }
    }

}