Computer >> कंप्यूटर >  >> प्रोग्रामिंग >> Java

जावा प्रोग्राम एक स्ट्रिंग से सभी सफेद जगहों को हटाने के लिए

इस लेख में, हम समझेंगे कि एक स्ट्रिंग से सभी व्हाइटस्पेस को कैसे हटाया जाए। स्ट्रिंग एक डेटाटाइप है जिसमें एक या अधिक वर्ण होते हैं और दोहरे उद्धरण चिह्नों ("") में संलग्न होते हैं।

नीचे उसी का एक प्रदर्शन है -

मान लीजिए कि हमारा इनपुट है -

Input string: Java programming is fun to learn.

वांछित आउटपुट होगा -

The string after replacing white spaces: Javaprogrammingisfuntolearn.

एल्गोरिदम

Step 1 - START
Step 2 - Declare two strings namely String input_string and result.
Step 3 - Define the values.
Step 4 - Use the function replaceAll("\\s", "") to replaces all the white spaces with blank spaces.
Step 5 - Display the result
Step 6 - Stop

उदाहरण 1

यहां, हम 'मेन' ​​फंक्शन के तहत सभी ऑपरेशंस को एक साथ बांधते हैं।

public class Demo {
   public static void main(String[] args) {
      String input_string = "Java programming is fun to learn.";
      System.out.println("The string is defined as: " + input_string);
      String result = input_string.replaceAll("\\s", "");
      System.out.println("\nThe string after replacing white spaces: " + result);
   }
}

आउटपुट

The string is defined as: Java programming is fun to learn.

The string after replacing white spaces: Javaprogrammingisfuntolearn.

उदाहरण 2

यहां, हम ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग को प्रदर्शित करने वाले कार्यों में संचालन को समाहित करते हैं।

public class Demo {
   public static String string_replace(String input_string){
      String result = input_string.replaceAll("\\s", "");
      return result;
   }
   public static void main(String[] args) {
      String input_string = "Java programming is fun to learn.";
      System.out.println("The string is defined as: " + input_string);
      String result = string_replace(input_string);
      System.out.println("\nThe string after replacing white spaces: " + result);
   }
}

आउटपुट

The string is defined as: Java programming is fun to learn.

The string after replacing white spaces: Javaprogrammingisfuntolearn.

  1. सूची से डुप्लिकेट तत्वों को हटाने के लिए जावा प्रोग्राम

    किसी सूची से डुप्लीकेट हटाने के लिए, कोड इस प्रकार है - उदाहरण आयात करें (); list.add (जैकब); सूची जोड़ें (गैरी); सूची जोड़ें (गैरी); सूची जोड़ें (हैरी); सूची जोड़ें (हैरी); list.add (केविन); System.out.println (सूची = + सूची); सेट सेट =नया लिंक्ड हैशसेट (सूची); System.out.println (डुप्लिकेट तत्वो

  1. जावा में एक स्ट्रिंग के सभी गैर-वर्णमाला वर्ण निकालें?

    विभाजन () स्ट्रिंग वर्ग की विधि सीमांकक का प्रतिनिधित्व करने वाले स्ट्रिंग मान को स्वीकार करती है और टोकन (शब्दों) की सरणी में विभाजित होती है, दो सीमांककों की घटना के बीच स्ट्रिंग को एक टोकन के रूप में मानती है। उदाहरण के लिए यदि आप इस पद्धति के लिए एक सीमांकक के रूप में एकल स्थान पास करते हैं और