Removing a specific space in a string

6

I'm having trouble implementing replace in String in Android Studio.

I need to remove some white space in String .

Real examples:

  • 8 hrs 2 mins
  • 1 day 2 mins

I need the text to look like this:

  • 8hrs 2mins
  • 1day 2mins

If someone can help me, thank you.

    
asked by anonymous 16.12.2015 / 12:21

4 answers

1

As you said that the format can be varied ( min , mins , miNs , etc ...) I'll post another way of doing considering spaces and not words.

You can use the Scanner for this, the staff only remembers to read with System.in but it has several useful resources. Scanner#next() returns the next incoming token, and these tokens are, by default, separated by spaces.

If the input format is always the one you posted, then you can use next() + next() to get the tokens two at a time. It would look like this:

TOKENS:        8      |   hrs    |    2    |   mins

               ^           ^          ^         ^
               |           |          |         |
               |           |          |         |
CHAMADAS:    next()  +   next()  |  next()  +  next()

                     ^                      ^
                     |                      |

RESULTADO:          8hrs         |        2mins

I made a method with this logic:

public String format(String string){
   Scanner scanner = new Scanner(string);
   StringBuilder sb = new StringBuilder();
   while(scanner.hasNext())
      sb.append(scanner.next())
        .append(scanner.next())
        .append(" ");
   return sb.toString();
}

And in the tests the results were:

String test1 = "50 min";
String test2 = "20 hrs 50 mins";
String test3 = "1 d 20 hrs 50 min";
String test4 = "3 meses 15 dias 20 horas 50 minutos";

System.out.println(format(test1)); // 50min 
System.out.println(format(test2)); // 20hrs 50mins 
System.out.println(format(test3)); // 1d 20hrs 50min 
System.out.println(format(test4)); // 3meses 15dias 20horas 50minutos 

Example online

    
16.12.2015 / 15:51
10
String comEspaco = "8 hrs 2 mins";
String semEspaco = comEspaco.replace(" hr", "hr").replace(" min", "min");

Workaround with regex without depending on what comes after each space:

String comEspaco = "8 hrs 2 mins";
String semEspaco = comEspaco.replaceAll("([0-9]) ", "$1");

See working at Ideone .

    
16.12.2015 / 12:34
2

One possible way is to use a StringBuffer and use the replace() method:

String hora = "8 hrs 2 mins";
int firstSpace = hora.indexOf(" ");
int lastSpace = hora.lastIndexOf(" ");
StringBuffer buf = new StringBuffer(hora);

buf.replace(firstSpace, firstSpace + 1, "");
buf.replace(lastSpace-1, lastSpace, "");//Note que após o replace anterior o buffer tem menos um caracter

System.out.println(buf.toString());

See it working on Ideone

Another way using subString()

Declare a method that does the replace in a certain position:

public static String replaceCharAt(String s, int pos, String c) {
    return s.substring(0, pos) + c + s.substring(pos + 1);
}

Use it as follows:

String hora = "8 hrs 2 mins";
int firstSpace = hora.indexOf(" ");

String temp = replaceCharAt(hora, firstSpace, "");

int lastSpace = temp.lastIndexOf(" ");
String horaSemEspacos = replaceCharAt(temp, lastSpace, "");

System.out.println(horaSemEspacos);

See it working on Ideone

    
16.12.2015 / 13:06
2
String comEspaco = "8 hrs 2 mins";
String semEspaco;

if (comEspaco.constains("hrs")){
    semEspaco = comEspaco.replace(" hrs", "hrs").replace(" mins", "mins")
} else{
    semEspaco = comEspaco.replace(" dia", "dia").replace(" hrs", "hrs")
}
    
16.12.2015 / 12:58