GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Java-Programs/Other-Java-Programs/RemoveWordFromString.java at master · iamarav/Java-Programs

Expand file tree

/

RemoveWordFromString.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
82 lines (63 loc) · 2.15 KB
/

RemoveWordFromString.java

Copy path

File metadata and controls

82 lines (63 loc) · 2.15 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.util.Scanner;
public class RemoveWordFromString {
public static void main(String[] args) {
System.out.println("Program to remove a word from String.\n---");
String string = "", word = "";
Scanner sm = new Scanner(System.in);
System.out.println("Enter the String: ");
string = sm.nextLine();
System.out.print("Enter the word to remove from String: ");
word = sm.nextLine();
if (string.isEmpty() || string.equals(" ")) { //To check if String is empty.
System.out.println("The String is empty");
end();
}
else if (word.isEmpty() || word.equals(" ")) { //To check if word is empty.
System.out.println("The Word entered is blank.");
end();
}
if(removeWord(string, word)) {
end();
}
sm.close();
}
private static boolean removeWord(String string, String word) {
boolean bb = true;
if (checkWord(string, " "+word)) { //for words between Strings
//Now Remove the word from String//
string = string.replaceAll(" "+word, "");
System.out.println("Word has been removed from String.");
System.out.println("Output String: "+string);
}
else if (checkWord(string, word+" ")) { //for words at the starting of string
//Now Remove the word from String//
string = string.replaceAll(word+" ", "");
System.out.println("Word has been removed from String.");
System.out.println("Output String: "+string);
}
else if (checkWord(string, word)) { //for words at the end of string
//Now Remove the word from String//
string = string.replaceAll(word, "");
System.out.println("Word has been removed from String.");
System.out.println("Output String: "+string);
}
else {
System.out.println("Word not found in String");
System.out.println("Output String: "+string);
end();
}
return bb;
}
private static boolean checkWord(String string, String word) { //Function to check if word exist in String
boolean flag = false;
if (string.contains(word)) {
System.out.println("Word found in String");
flag = true;
}
return flag;
}
private static void end() { //Function to end Program
System.out.println("---\nThe Program has ended.");
System.exit(0);
}
}
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.