Selama pemrosesan tekstual, apakah Anda mencari kata-kata tertentu dan membuat aturan pencocokan pola, menghitung frekuensi elemen, dll. - tanda baca dapat mengacaukan rencana Anda Show
Seringkali, Anda ingin menghapus kata henti, tanda baca, angka, atau beberapa kategori karakter, bergantung pada tujuan akhir Anda.
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 Anda harus menghindari garis miring terbalik pertama dalam sebuah string, jadi menghapus semua tanda baca sama dengan mencocokkannya dan menggantinya dengan karakter kosong
Mari kita terapkan pada kalimat sederhana _Ini menghasilkan
Mari kita lihat karakter apa saja yang diperlakukan sebagai tanda baca di sini
Dengan karakter khusus ini - mana yang tersisa setelah tanda baca dihapus? _Hapus Tanda Baca dari String tanpa RegExJika Anda tidak ingin menggunakan ekspresi reguler, Anda dapat melakukan pemeriksaan manual sambil mengulangi setiap karakter string. Ingatlah untuk menggunakan 0 alih-alih 1 saat melakukan ini, karena string tidak dapat diubah dan salinan harus dibuat setiap kali Anda ingin menambahkan karakter - jadi Anda akan membuat 2 jumlah string dalam memori 0 dapat diubah, dan dapat dengan mudah diubah menjadi string yang tidak dapat diubah di akhir prosesLihat 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 _Mari buat string dan bersihkan
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. KesimpulanDalam 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 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
Di bawah ini adalah implementasi dari pendekatan di atas C++
________ 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 went1 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went2 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went1 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went4 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went5 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went6
Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went1 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went8 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went9 int Welcome to GeeksforGeeks1 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went1 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went0 ________25 _25_______5Welcome to GeeksforGeeks4 Welcome to GeeksforGeeks7 Welcome to GeeksforGeeks8 ________25 _14_______0
________25 _128_______6________14 _128_______6Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went_1 ________14 _129_______1________14 _129_______3Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went1 #include <iostream> 5 #include <iostream> 6
Jawa
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 went1 #include <iostream> 9 using 5 using 6 using 7Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went1 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went0 ________25 _14_______2Welcome to GeeksforGeeks4 namespace 3Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went5 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went6 Welcome to GeeksforGeeks_4 ________25 _131_______8Welcome to GeeksforGeeks4 std; 0std; 1std; 2std; 3std; 4Welcome to GeeksforGeeks_4 ________25 _132_______7________14 _128_______6Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went_1
Python3
________14 _133_______8Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went1 main() 0main() 1 main() 2
________14 _134_______4Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went1 main() 6Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went1 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went8 main() 9Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went00 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went01 Welcome to GeeksforGeeks4 Welcome to GeeksforGeeks7 main() 9Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went00 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went06
Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went08 main() 1 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went10
Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went1 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went12 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went1 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went14 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went15
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 went08 main() 1 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went5 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
Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went24
Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went26
Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went28 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 went33 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 went1 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went2 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went1 namespace 3Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went5 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went6 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went_1 ________14 _131_______8Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went1 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went45 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went46 std; 2std; 3std; 4Input : %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 went1 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went52
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 JavascriptInput : %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 went1 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went0 ________25 _14_______2Welcome to GeeksforGeeks4 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went64 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went65 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went5________14______6 Welcome to GeeksforGeeks_4 ________25 _131_______8Welcome to GeeksforGeeks4 Input : %welcome' to @geeksforgeek<s Output : welcome to geeksforgeeks Input : Hello!!!, he said ---and went. Output : Hello he said and went72 std; 3std; 4Welcome to GeeksforGeeks_4 ________25 _14_______77________14 _128_______6Input : %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) 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. |