2

I want to replace 1 by 1Min, 5 by 5Min, 10 by 10Min, 15 by 15Min,60 by 1Hour, 360 by 6Hours etc.

How can I do it in 1 statement.

String str = "15";
String newStr = str.replace("1", "1Min").replace("5", "5Mins").replace("10", "10Mins").replace("15", "15Mins").replace("30", "30Mins").replace("60", "1Hr").replace("120", "2Hrs").replace("240", "4Hrs").replace("480", "8Hrs").replace("720", "12Hrs").replace("1440", "24Hrs");

If I try this, i get '1Min5Mins' because it replaces 1 and then 5.

This could be a stupid question, I can do it with switch or if, else-if. just wondering can this be done in 1 line code. Thanks

2
  • What do you do with 80? Commented Nov 23, 2015 at 15:01
  • Nothing... I know there will be only these values (1,5,10,15,30,60,120,240,480,600,720,1440) Commented Nov 23, 2015 at 15:06

4 Answers 4

6

You could try something like this:

String str = "15";
int minutes = Integer.parseInt(str);
if (minutes < 60)
    str += "Mins";
else
    str = (minutes/60) + "Hrs";

but be aware that Integer.parseInt() will throw an exception if you try to parse anthing else than a number. This can be inaccurate if the hours are not exact (365 will be replaced by 6 hours although it's 6 hours and 5 minutes)

Here's the 1-liner:

str = Integer.parseInt(str) < 60 ? str+"Mins" : (Integer.parseInt(str)/60) + (Integer.parseInt(str)/60 == 1 ? "Hr" : "Hrs");

although this would be more readable with a 2-liner:

int minutes = Integer.parseInt(str);
str = minutes < 60 ? str+"Mins" : (minutes/60) + (minutes/60 == 1 ? "Hr" : "Hrs");
Sign up to request clarification or add additional context in comments.

8 Comments

This is not 1 line code sir :) I know I can do it this way.
@Ana i'll write you a 1 liner but it's not very pretty then ;)
Great... Thanks for this, i think this is more elegant :)
@Ana more importantly it's a lot more readable and should also be way faster!
But for 60 it will give 60Min instead of 1 Hr
|
3

Here is the one-liner for you:

String newStr = str.replaceAll("\\b1\\b", "1Min").replaceAll("\\b5\\b", "5Mins").replace("10", "10Mins").replace("15", "15Mins").replace("30", "30Mins").replace("60", "1Hr").replace("120", "2Hrs").replace("240", "4Hrs").replace("480", "8Hrs").replace("720", "12Hrs").replace("1440", "24Hrs");

With the regex word boundaries. Was too lazy to finish it till the end.

2 Comments

This is awesome. Thanks :)
@Ana np, pls go ahead and mark this as an answer so the lazy way of coding shows on top :)
2

You could adjust for the earlier replacements when looking for later replacements:

String str = "15";
String newStr = str.replace("1", "1Min").replace("5", "5Mins").replace("1Min0", "10Mins").replace("1Min5Mins", "15Mins").replace("30", "30Mins")
                       .replace("60", "1Hr").replace("1Min20", "2Hrs").replace("240", "4Hrs").replace("480", "8Hrs").replace("720", "12Hrs")
                       .replace("1Min440", "24Hrs");

This way, the 1 in 15 will first be replaced by 1Min, then the 5 will be replaced by 5Mins and then 1Min5Mins will be replaced by 15Mins.

It is not the most elegant solution though, but it is the only way I can think of to replace everything in one line.

1 Comment

I really like what you are doing there :D +1 for nice-ugly code ;)
1

This may not be a one liner but it is a dynamic approach to your problem:

public static void main(String[] args) throws FileNotFoundException {
    String test = "15 20 60 360";
    Pattern pattern = Pattern.compile("\\d+");

    Matcher matcher = pattern.matcher(test);
    while(matcher.find())
    {
        int temp = Integer.parseInt(matcher.group(0));
        if(temp >= 60)
        {
            int temp1 = temp / 60;
            test = test.replaceFirst(("" + temp), ("" + temp1 + "Hour(s)"));
        }
        else
        {
            test = test.replaceFirst(("" + temp), ("" + temp + "Min(s)"));
        }
    }
    System.out.println(test);
}

Output:

15Min(s) 20Min(s) 1Hour(s) 6Hour(s)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.