How to Replace a Word in a Sentence in Java
- 1). Open your Java code in your development program of choice.
- 2). Locate the string variable that holds the sentence you want to process. In this example, the string variable name is "sentence" and the string contained in it is "This is a sample sentence."
- 3). Type, on a new line, the following code: string ReplaceSubstring(sentence, "is", "isn't");. This code takes the "sentence" variable and replaces the occurrence of "is" in the sentence with the word "isn't" and automatically updates the "sentence" variable.
Source...