Javascript menghapus tanda baca dari akhir string

Selama pemrosesan tekstual, apakah Anda mencari kata-kata tertentu dan membuat aturan pencocokan pola, menghitung frekuensi elemen, dll. - tanda baca dapat mengacaukan rencana Anda

Seringkali, Anda ingin menghapus kata henti, tanda baca, angka, atau beberapa kategori karakter, bergantung pada tujuan akhir Anda.

Dalam tutorial singkat ini, kita akan melihat cara menghapus tanda baca dari sebuah string di Java

Hapus Tanda Baca dari String dengan RegEx (Regular Expressions)

Ekspresi Reguler sangat cocok di sini, baik karena kemungkinan besar akan menjadi bagian dari bagian pemrosesan lainnya, dan karena merupakan pencocokan pola yang efisien. Di Jawa, ekspresi reguler untuk mencocokkan tanda baca adalah \p{Punct} atau singkatan \p{P}

Anda harus menghindari garis miring terbalik pertama dalam sebuah string, jadi menghapus semua tanda baca sama dengan mencocokkannya dan menggantinya dengan karakter kosong

String.replaceAll("\\p{P}", "")

Mari kita terapkan pada kalimat sederhana

String text = "Hi! This is, in effect, a synthetic sentence. It's meant to have several punctuation characters!";
String clean = text.replaceAll("\\p{P}", "");
System.out.println(clean);
_

Ini menghasilkan

Hi This is in effect a synthetic sentence Its meant to have several punctuation characters

Mari kita lihat karakter apa saja yang diperlakukan sebagai tanda baca di sini

String text = "!#$%&'()*+,-./:;<=>[email protected][]^_`{|}~";
String clean = text.replaceAll("\\p{P}", "");
System.out.println(clean);

Dengan karakter khusus ini - mana yang tersisa setelah tanda baca dihapus?

$+<=>^`|~
_

Hapus Tanda Baca dari String tanpa RegEx

Jika Anda tidak ingin menggunakan ekspresi reguler, Anda dapat melakukan pemeriksaan manual sambil mengulangi setiap karakter string. Ingatlah untuk menggunakan

String text = "Hi! This is, in effect, a synthetic sentence. It's meant to have several punctuation characters!";
String clean = text.replaceAll("\\p{P}", "");
System.out.println(clean);
0 alih-alih
String text = "Hi! This is, in effect, a synthetic sentence. It's meant to have several punctuation characters!";
String clean = text.replaceAll("\\p{P}", "");
System.out.println(clean);
1 saat melakukan ini, karena string tidak dapat diubah dan salinan harus dibuat setiap kali Anda ingin menambahkan karakter - jadi Anda akan membuat
String text = "Hi! This is, in effect, a synthetic sentence. It's meant to have several punctuation characters!";
String clean = text.replaceAll("\\p{P}", "");
System.out.println(clean);
2 jumlah string dalam memori

String text = "Hi! This is, in effect, a synthetic sentence. It's meant to have several punctuation characters!";
String clean = text.replaceAll("\\p{P}", "");
System.out.println(clean);
0 dapat diubah, dan dapat dengan mudah diubah menjadi string yang tidak dapat diubah di akhir proses

Lihat panduan praktis dan praktis kami untuk mempelajari Git, dengan praktik terbaik, standar yang diterima industri, dan menyertakan lembar contekan. Hentikan perintah Googling Git dan benar-benar pelajari itu

public static String removePunctuations(String s) {
    StringBuffer buffer = new StringBuffer();
    for (Character c : s.toCharArray()) {
        if(Character.isLetterOrDigit(c))
            buffer.append(c);
    }
    return buffer.toString();
}
_

Mari buat string dan bersihkan

String text = "Hello! \nHere are some special characters: !#$%&'()*+,-./:;<=>[email protected][]^_`{|}~ \nWhere are they? :(\n";
System.out.println(text);
String clean = removePunctuations(text);
System.out.println(clean);
Hello! 
Here are some special characters: !#$%&'()*+,-./:;<=>[email protected][]^_`{|}~ 
Where are they? :(

HelloHerearesomespecialcharactersWherearethey

Meskipun proses ini lebih dapat disesuaikan, proses ini hanya memeriksa huruf dan angka. Anda dapat memeriksa kode karakter secara manual sebagai alternatif, dan hanya mengecualikan beberapa karakter tanda baca saja - dan membiarkannya di spasi putih, ganti baris, dll.

Kesimpulan

Dalam tutorial singkat ini, kita melihat bagaimana Anda dapat menghapus tanda baca atau karakter khusus tertentu dari sebuah string di Java, menggunakan ekspresi reguler atau pemeriksaan manual dalam loop

String text = "Hi! This is, in effect, a synthetic sentence. It's meant to have several punctuation characters!";
String clean = text.replaceAll("\\p{P}", "");
System.out.println(clean);
4 yang disempurnakan

Diberi string, hapus tanda baca dari string jika karakter yang diberikan adalah karakter tanda baca, seperti yang diklasifikasikan oleh lokal C saat ini. Lokal C default mengklasifikasikan karakter ini sebagai tanda baca.  

! " # $ % & ' ( ) * + , - . / : ; ? @ [ \ ] ^ _ ` { | } ~ 

Contoh.  

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went

Direkomendasikan. Harap coba pendekatan Anda pada {IDE} terlebih dahulu, sebelum melanjutkan ke solusi

Mendekati. Pertama periksa string input jika terdiri dari tanda baca maka kita harus membuatnya bebas tanda baca. Untuk melakukan ini, kami akan melintasi string, dan jika tanda baca ditemukan, kami akan menghapusnya. Katakanlah input string adalah '$Student@' maka kita harus menghapus $ dan @, selanjutnya kita harus mencetak string polos 'Student' yang bebas dari tanda baca apapun

Algoritma

  1. Inisialisasi string input
  2. Periksa apakah karakter yang ada dalam string adalah tanda baca atau tidak
  3. Jika sebuah karakter adalah tanda baca, maka hapus karakter tersebut dan kurangi indeksnya
  4. Cetak string keluaran, yang akan bebas dari tanda baca apa pun

Di bawah ini adalah implementasi dari pendekatan di atas

C++




// CPP program to remove punctuation from a given string

 

#include <iostream>

using namespace std;

 

________ 133 _______ _______ 134 _______

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
_0

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
1
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
2

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
1
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
4
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
5
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
6

 

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
1
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
8
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
9int
Welcome to GeeksforGeeks
1

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
1
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
0

________25

$+<=>^`|~
_25_______5

Welcome to GeeksforGeeks
4
Welcome to GeeksforGeeks
7
Welcome to GeeksforGeeks
8

________25

$+<=>^`|~
_14_______0

// CPP program to remove punctuation from a given string1// CPP program to remove punctuation from a given string2

// CPP program to remove punctuation from a given string1// CPP program to remove punctuation from a given string4

________25

$+<=>^`|~
_128_______6

________14

String text = "Hi! This is, in effect, a synthetic sentence. It's meant to have several punctuation characters!";
String clean = text.replaceAll("\\p{P}", "");
System.out.println(clean);
_128_______6

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
_1

________14

String text = "Hi! This is, in effect, a synthetic sentence. It's meant to have several punctuation characters!";
String clean = text.replaceAll("\\p{P}", "");
System.out.println(clean);
_129_______1

________14

String text = "Hi! This is, in effect, a synthetic sentence. It's meant to have several punctuation characters!";
String clean = text.replaceAll("\\p{P}", "");
System.out.println(clean);
_129_______3

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
1#include <iostream>5 #include <iostream>6

// CPP program to remove punctuation from a given string_6

Jawa




#include <iostream>_8

 

#include <iostream>9 using0 using1

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
_0

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
1#include <iostream>9 using5 using6 using7

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
1
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
0

________25

$+<=>^`|~
_14_______2

Welcome to GeeksforGeeks
4namespace3
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
5
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
6

Welcome to GeeksforGeeks
_4

________25

$+<=>^`|~
_131_______8

Welcome to GeeksforGeeks
4std;0std;1std;2std;3std;4

Welcome to GeeksforGeeks
_4

________25

$+<=>^`|~
_132_______7

________14

String text = "Hi! This is, in effect, a synthetic sentence. It's meant to have several punctuation characters!";
String clean = text.replaceAll("\\p{P}", "");
System.out.println(clean);
_128_______6

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
_1

// CPP program to remove punctuation from a given string_6

int_2

Python3




int_3

int_4

int5 int6

 

________14

String text = "Hi! This is, in effect, a synthetic sentence. It's meant to have several punctuation characters!";
String clean = text.replaceAll("\\p{P}", "");
System.out.println(clean);
_133_______8

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
1main()0main()1 main()2

 

________14

String text = "Hi! This is, in effect, a synthetic sentence. It's meant to have several punctuation characters!";
String clean = text.replaceAll("\\p{P}", "");
System.out.println(clean);
_134_______4

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
1main()6

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
1
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
8 main()9
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
00
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
01

Welcome to GeeksforGeeks
4
Welcome to GeeksforGeeks
7 main()9
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
00
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
06

// CPP program to remove punctuation from a given string1

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
08main()1
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
10

 

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
1
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
12

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
1
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
14
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
15

 

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
_16

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
08main()1
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
5

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
_20

C#




Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
_21

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
_22

using

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
24

using

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
26

 

using0

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
28

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
_0

#include <iostream>9 using5 using6

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
33

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
_0

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
1
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
2

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
1namespace3
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
5
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
6

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
_1

________14

String text = "Hi! This is, in effect, a synthetic sentence. It's meant to have several punctuation characters!";
String clean = text.replaceAll("\\p{P}", "");
System.out.println(clean);
_131_______8

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
1
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
45
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
46std;2std;3std;4

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
_1

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
1
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
52

// CPP program to remove punctuation from a given string_6

// CPP program to remove punctuation from a given string_6

 

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
_55

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
_56

Javascript




Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
_57

 

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
_58

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
1
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
0

________25

$+<=>^`|~
_14_______2

Welcome to GeeksforGeeks
4
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
64
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
65
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
5________14______6

Welcome to GeeksforGeeks
_4

________25

$+<=>^`|~
_131_______8

Welcome to GeeksforGeeks
4
Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
72std;3std;4

Welcome to GeeksforGeeks
_4

________25

$+<=>^`|~
_14_______77

________14

String text = "Hi! This is, in effect, a synthetic sentence. It's meant to have several punctuation characters!";
String clean = text.replaceAll("\\p{P}", "");
System.out.println(clean);
_128_______6

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
_1

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
_81

 

Input : %welcome' to @geeksforgeek<s
Output : welcome to geeksforgeeks

Input : Hello!!!, he said ---and went.
Output : Hello he said and went
_82

Keluaran

Welcome to GeeksforGeeks

Kompleksitas Waktu. O(n2)
Ruang Bantu. O(1)

Artikel ini disumbangkan oleh Aarti_Rathi dan Pramod Kumar. Jika Anda menyukai GeeksforGeeks dan ingin berkontribusi, Anda juga dapat menulis artikel menggunakan tulis. geeksforgeeks. org atau kirimkan artikel Anda ke review-team@geeksforgeeks. org. Lihat artikel Anda muncul di halaman utama GeeksforGeeks dan bantu Geeks lainnya.  

Bagaimana cara menghapus tanda baca dari akhir string JavaScript?

Kita dapat menggunakan metode penggantian string JavaScript dengan regex yang cocok dengan pola dalam string yang ingin kita ganti . Jadi kita bisa menggunakannya untuk menghapus tanda baca dengan mencocokkan tanda baca dan menggantinya dengan string kosong.

Bagaimana cara menghapus tanda baca di string Java?

Di bawah ini adalah implementasi dari pendekatan di atas. C++ Jawa. .
Inisialisasi string input
Periksa apakah karakter yang ada dalam string adalah tanda baca atau tidak
Jika sebuah karakter adalah tanda baca, maka hapus karakter tersebut dan kurangi indeksnya
Cetak string keluaran, yang akan bebas dari tanda baca apa pun

Bagaimana Anda membersihkan tanda baca dari sebuah string?

Metode 1. Hapus Tanda Baca dari String dengan Terjemahan . Ini menginstruksikan metode Python untuk menghilangkan tanda baca dari sebuah string. Ini adalah salah satu cara terbaik untuk menghapus tanda baca dari string.

Mengapa tanda baca dalam teks harus dihilangkan?

Menghapus Tanda Baca . Misalnya kata data dan data. diperlakukan sama setelah proses penghapusan tanda baca. help to treat each text equally. For example, the word data and data! are treated equally after the process of removal of punctuations.