从字符串中删除确切的单词可以通过以下步骤实现:
以下是一个示例代码,演示如何从字符串中删除确切的单词:
public class RemoveWordFromString {
public static void main(String[] args) {
String sentence = "This is a sample sentence.";
String wordToRemove = "sample";
// Step 1: Split the sentence into an array of words
String[] words = sentence.split(" ");
// Step 2 and 3: Remove the exact word from the array
for (int i = 0; i < words.length; i++) {
if (words[i].equals(wordToRemove)) {
// Remove the word from the array
words[i] = "";
}
}
// Step 4: Reconstruct the sentence without the removed word
String modifiedSentence = String.join(" ", words);
System.out.println(modifiedSentence);
}
}
输出结果为:"This is a sentence."
腾讯云相关产品和产品介绍链接地址:
请注意,以上只是腾讯云的一些产品示例,其他云计算品牌商也提供类似的产品和服务。
领取专属 10元无门槛券
手把手带您无忧上云