2
String mine = sc.next();
        String corrected = mine.replace('.', '????');
        System.out.println(corrected);

that's my code. let's assume that my input on String corrected is "<..><.<..>>" , and I want to replace every "." with a null space, so I get an output like "<><<>>". is there any way to do it?

1
  • 6
    just use an empty string mine.replace('.', '') Commented May 16, 2020 at 0:40

3 Answers 3

5

If you want to replace . with an empty ("") string, you can just do:

mine.replace(".", "");

Alternatively, you can also check .replaceAll()

Sign up to request clarification or add additional context in comments.

2 Comments

oh this actually worked, i was using these ' ' instead of " ", so I was getting an error. Thanks =)
String#replace takes either a char or a CharSequence.
2

Try this to replace all occurrences of . with empty:

mine.replaceAll("\\.", "")

Comments

0

If you don't want any method, you can do it like this.

String str = "<<.>>.<>.<<.";
String [] parts = str.split("\\.");

for(String s:parts){
    System.out.print(s);
}

Because I tried the method replaceAll(".", "") ; But that method does not allow empty or null spaces in a string. I don't know if it's the best way, but that's what I can think of.

1 Comment

Split() is a method of String.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.