B
B
Bakotiinii2018-01-29 13:09:51
Java
Bakotiinii, 2018-01-29 13:09:51

How to format strings in Java with substrings?

We are given a string of a certain length.
Task: parse the given string into substrings, insert the necessary elements between them and glue everything again.
I'm trying something like this, but the method itself says that it cannot be used outside of the package and stuff. I used to write in python, it was easier cleanNumberх[0:3], but in Java I don’t understand how to pull it out correctly.

public void initNumber() {
        cleanNumber = "xxxxxxx";
        number = String.format("%s %s %s-%s-%s", cleanNumber.getChars(0, 3)....);
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kastian, 2018-01-29
@iSmoke

It depends on what "parse into substrings" means. By what criterion? If for some repeated words, symbols, spaces, you can use replaceall (if you insert the same thing). If different, split the string into an array of strings using split, and then assemble it into a StringBuilder with the necessary inserts.

K
Kirill Romanov, 2018-01-29
@Djaler

https://docs.oracle.com/javase/7/docs/api/java/lan...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question