how to change the string into a table and replace all punctuation marks

advertisements

This question already has an answer here:

  • How to split a string in Java 28 answers
  • Java: Remove full stop in string 5 answers

i have a String and i want change it to Array next show only split words in one line. I have not idea how to do it my only idea is to do this like that :

public static void main(String[] args) {
    String ciag="Hello My name is Tomek, i am from Mars.";
    ciag=ciag.replaceAll(",", "").replaceAll(".", "");
    System.out.println(ciag);

but it is wrong too any advice's


This will work.

String ciag="Hello My name is Tomek, i am from Mars.";
ciag=ciag.replace(",", "").replace(".", "");
System.out.println(ciag);
String[] arr = ciag.split("\\s+");

replaceAll() uses RegEx to replace and in RegEx . stands for any character. Use replace().

split() also takes a RegEx and \s+ matches one or more occurrences of a whitespace.