如何将句子(字符串)中的单词更改为标题案例,该句子中的连词应留在或更改为较低字母

发布于 2025-01-22 18:58:59 字数 672 浏览 0 评论 0原文

我需要知道如何将句子(字符串)中的单词更改为标题案例,句子中的连词必须保留或更改为较低字母(所有单词)。

类似于str_to_title(句子,locale =“ en”),从字符串库中,但该函数识别连词(for和the,from等)。 最常见的协调连词是:对于,也是,或者,或者,或者如此。

同样,重要的是, 一词保持较低的情况,而不是第一个单词。第一个和遗言应始终在上案中具有首字母。

例如:

sentence = "THE LOVer Tells OF THE rose in HIS HEART"

我希望字符串句子更改为:

"The Lover Tells of the Rose in His Heart" 

称为a 标题案例

任何帮助都将受到赞赏

I need to know how to change the words in a sentence (string) to Title Case, where the conjunctions in the sentence have to stay or change to lower letters (all the word).

Something similar to str_to_title(sentence, locale = "en"), from the string library, but in which the function recognizes conjunctions (for, and, the, from, etc.).
The most common coordinating conjunctions are: for, and, nor, but, or, yet, and so.

Also, it is important that the word the stay as lower case but not when is the first word. The first and last words should always have the first letter in upper case.

For example:

sentence = "THE LOVer Tells OF THE rose in HIS HEART"

I want the string sentence to change to:

"The Lover Tells of the Rose in His Heart" 

Which is called a Title Case.

Any help is appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人的夜不怕黑 2025-01-29 18:58:59

该解决方案怎么样?
我从编程中弄清楚了您的问题。

sentence <- "THE LOVer Tells OF THE rose in HIS HEART"

require(stringr)

words <- unlist(strsplit(sentence,' ')) %>% tolower()
conjunctions <- c('for', 'and', 'nor', 'but', 'or', 'yet','the','of','in')
for(i in seq_len(length(words))){
  if(i==1 & words[i] %in% conjunctions){
    words[i] <- str_to_title(words[i])
  } else if (!words[i] %in% conjunctions) {
    words[i] <- str_to_title(words[i])
  }
}
words
result <- paste(words, collapse=' ')
result
  1. 将句子拆分为单词,并使它们Tolower
  2. 除了第一个单词以外的协调连词将传递,而其他单词将str_to_title
  3. 粘贴单词中的句子。

结果将是
[1]“爱人在他心中讲述玫瑰”

How about this solution?
I figured your question out of programming.

sentence <- "THE LOVer Tells OF THE rose in HIS HEART"

require(stringr)

words <- unlist(strsplit(sentence,' ')) %>% tolower()
conjunctions <- c('for', 'and', 'nor', 'but', 'or', 'yet','the','of','in')
for(i in seq_len(length(words))){
  if(i==1 & words[i] %in% conjunctions){
    words[i] <- str_to_title(words[i])
  } else if (!words[i] %in% conjunctions) {
    words[i] <- str_to_title(words[i])
  }
}
words
result <- paste(words, collapse=' ')
result
  1. Split a sentence to words and and make them tolower.
  2. Coordinating conjunctions ,except the first word, would be passed and other words would str_to_title.
  3. paste the words into a sentence.

The result would be
[1] "The Lover Tells of the Rose in His Heart"

北渚 2025-01-29 18:58:59

使用Library(工具)加载软件包,使用TotitleCase函数转换您的字符串TotitleCase(句子),并且完成了!请注意,此功能仅适用于英语,而不适用于其他任何语言。

Load the package usinglibrary(tools), use the toTitleCase function to convert your string toTitleCase(sentence) and it's done! Just be mindful that this function only works for English, not any other languages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文