【问题标题】:I am stuck on trying to return a certain part of a string because its a variable我一直试图返回字符串的某个部分,因为它是一个变量
【发布时间】:2019-09-07 23:42:43
【问题描述】:

基本上,对于我分配的程序,我必须“ "通过从每个句子中检索最后一个单词来创建一个字符串,删除除最后一个单词之外的所有标点符号,然后输出这个新字符串"

我一直在检索最后一个单词,因为字符串内部有多个变量,这些变量会根据用户输入更改整个字符串的索引。

我尝试过使用索引并使用 indexOf,但老实说,我碰壁了,我完全被卡住了

String Sen1 = " There once was a person named " + firstName + " who lived in a city called " + city + ". ";

System.out.println("new phrase is: " + Sen1.substring(62, Sen1.length() - 2));

如果我严格使用我的个人信息,该代码将起作用。但是如果另一个用户输入他们的信息,字符串的索引将会改变并且打印会出错。 我对这类问题使用了正确的方法吗? 有没有更简单的方法来做到这一点?

【问题讨论】:

  • 名字后面有分隔符吗?句子本身的格式是固定的吗?
  • 没有分隔符,它是固定的。整个问题是这样的:“在构建字符串之后,将字符串分成多个字符串,每个句子都在一个单独的变量中。然后通过从每个句子中检索最后一个单词来创建一个字符串,删除所有标点符号,除了最后一个单词,并输出这个新字符串。”
  • firstName 不会结束一个句子。 firstName.firstName, 会。

标签: java string variables substring return-type


【解决方案1】:

将您的输入字符串拆分为空格,并从结果字符串数组中返回最后一个字符串。 示例:

 public static void main(String []args){
    String variable = "randomlongwords";
    String sentence = "Hello this is a " + variable + " random sentence. ";
    String[] words = sentence.split(" ");
    System.out.println(words[words.length - 1]);
 }

输出:句子。

【讨论】:

    【解决方案2】:

    您要做的是利用String#split() 方法和Regular Expression 将您的段落分成句子。你可以这样做:

    String[] sentences = paragraph.split("\\.|\\!|\\?");
    

    但在您执行此操作之前,请抓住段落中使用的最后一个标点符号。显然,你会需要这个。

    现在您需要遍历 sentences 数组中的所有句子,然后将每个句子拆分为单独的 单词。从每个句子中取出最后一个单词并将它们全部连接成一个字符串变量(每个都用空格或其他分隔)。现在只需显示连接的字符串即可。它可能是这样的:

    String firstName = "Tom";
    String city = "Los Angeles";
    // A Paragraph (with indent)....
    String paragraph = "   In a world far far away there once was a person named " + firstName + 
                       " who lived in a city called " + city + ". He was a tall " +
                       "man with dark hair. He liked to drink the comb water in " + 
                       "hair salons!";
    // Trim off leading and trailing whitespaces, tabs, etc from your paragraph
    paragraph = paragraph.trim();
    // Collect the punctuation character used at the end of the paragraph.
    // Since this will be your last sentence anyway you will need it.
    String paragraphEndPunctuation = paragraph.substring(paragraph.length() - 1, paragraph.length());
    // Split the Paragraph into Sentences based on the major characters for sentence end.
    String[] sentences = paragraph.split("\\.|\\!|\\?");
    // Create a StringBuilder object so as to creat the string 
    // of detected last words in sentences.
    StringBuilder sb = new StringBuilder();
    // Iterate through each sentences and aquires the last word from each/
    for (int i = 0; i < sentences.length; i++) {
        // Split the current sentence into individual words
        String[] words = sentences[i].trim().split("\\s+");
        // Add the last word of current sentence to the StringBuilder object.
        // Ternary Operator is used here to apply a whitespace after each word
        // if the StringBuilder object already contains something.
        sb.append(sb.toString().equals("") ? words[words.length-1] : " " + words[words.length-1]);
    }
    // Append the final punctuation character.
    sb.append(paragraphEndPunctuation);
    // Display in console.
    System.out.println(sb.toString());
    

    【讨论】:

      猜你喜欢
      • 2017-07-08
      • 1970-01-01
      • 2018-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多