我如何获得介于两者之间的信息? "在用户输入的字符串?爪哇 [英] How do I get what's inbetween " " in a user inputted String? Java

查看:99
本文介绍了我如何获得介于两者之间的信息? "在用户输入的字符串?爪哇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检索某人作为字符串输入的引号中的所有内容,我假设它是我需要的子字符串,但我不确定如何使用。

I'd like to retrieve whatever is in quotes that someone enters as a string, i'm assuming it's substring that I need but i'm not sure how.

当用户输入一个由单词和数字混合而成的字符串时,它们之间都用一个空格隔开:
嘿110说我不太擅长Java,但我会很好地钓鱼

When the user inputs a string mixed with words and numbers all separated by one space: hey 110 say "I am not very good at Java" but " I can fish pretty well"

然后,我希望能够接受我不太擅长Java和我能很好地钓鱼,并打印出引号内的内容,以便可以在其中包含多个引号。串。
现在我有if(userInput ==')然后我用子字符串做些什么,但是我不确定是什么。

Then I want to be able to take the "I am not very good at Java" and the "I can fish pretty well" and print out what's inside the quotes so there can be multiple quotes in the string. right now I have if( userInput=='"') then I do something with substring but i'm not sure what.

我不能使用拆分,修剪,令牌生成器,正则表达式或任何其他可以使此操作真正轻松的事情。

I can't use split, trim, tokenizer, regex or anything that would make this really easy unfortunatley.

这都是在此方法中,我尝试确定字符串中是否有单词,数字或引号:

it's all in this method where I try to identify if something in the string is a word, number or a quote:

public void set(String userInput)// method set returns void
    {
        num=0;// reset each variable so new input can be passed

        String empty="";
        String wordBuilder="";
        userInput+=" ";
        for(int index=0; index<userInput.length(); index++)// goes through each character in string
        {

            if(Character.isDigit(userInput.charAt(index)))// checks if character in the string is a digit
            { 

                empty+=userInput.charAt(index);



            }
            else
            { 
                if (Character.isLetter(userInput.charAt(index)))
            {

                wordBuilder+=userInput.charAt(index);

            }
                else
                {
                    if(userInput.charAt(index)=='"')
                {
                    String quote=(userInput.substring(index,);

                }
                }
                //if it is then parse that character into an integer and assign it to num
                num=Integer.parseInt(empty);
                word=wordBuilder;


                empty="";
                wordBuilder="";
            }


        } 

    }


}

谢谢!

推荐答案

我不确定您是否正在寻找这个,但是它将逐步删除引用的部分...

I'm not sure if this quite what you are looking for, but it will strip down the quoted parts in steps...

String quote = "I say: \"I have something to say, \"It's better to burn out then fade away\"\" outloud...";

if (quote.contains("\"")) {

    while (quote.contains("\"")) {
        int startIndex = quote.indexOf("\"");
        int endIndex = quote.lastIndexOf("\"");
        quote = quote.substring(startIndex + 1, endIndex);
        System.out.println(quote);
    }

}

哪个输出...

I have something to say, "It's better to burn out then fade away"
It's better to burn out then fade away

已更新

我不知道这是否在作弊...

I don't know if this is cheating or not...

String quote = "I say: \"I have something to say, \"It's better to burn out then fade away\"\" outloud...\"Just in case you don't believe me\"";

String[] split = quote.split("\"");
for (String value : split) {
    System.out.println(value);
}

哪个输出...

I say: 
I have something to say, 
It's better to burn out then fade away

 outloud...
Just in case you don't believe me

已更新

好的,假的 String#split

StringBuilder sb = new StringBuilder(quote.length());
for (int index = 0; index < quote.length(); index++) {
    if (quote.charAt(index) == '"') {
        System.out.println(sb);
        sb.delete(0, sb.length());
    } else {
        sb.append(quote.charAt(index));
    }
}

已更新

好的,这基本上是伪造的 split ,带有选项...

Okay, this is basically fake split with options...

String quote = "blah blah 123 \"hello\" 234 \"world\"";

boolean quoteOpen = false;
StringBuilder sb = new StringBuilder(quote.length());
for (int index = 0; index < quote.length(); index++) {
    if (quote.charAt(index) == '"') {
        if (quoteOpen) {
            System.out.println("Quote: [" + sb.toString() + "]");
            quoteOpen = false;
            sb.delete(0, sb.length());
        } else {
            System.out.println("Text: [" + sb.toString() + "]");
            sb.delete(0, sb.length());
            quoteOpen = true;
        }
    } else {
        sb.append(quote.charAt(index));
    }
}
if (sb.length() > 0) {
    if (quoteOpen) {
        System.out.println("Quote: [" + sb.toString() + "]");
    } else {
        System.out.println("Text: [" + sb.toString() + "]");
    }
}

哪个生成...

Text: [blah blah 123 ]
Quote: [hello]
Text: [ 234 ]
Quote: [world]

知道,我不知道您是如何存储结果的。我很想创建一些能够存储 String 结果并将其添加到 List 的基本类。我可以维持顺序,甚至可以使用某种标志来确定它们是什么类型...

Know, I don't know how you are storing the results. I would be tempted to create some basic classes which were capable of storing the String results and add them to a List so I could maintain the order and maybe use a flag of some kind to determine what type they are...

这篇关于我如何获得介于两者之间的信息? &quot;在用户输入的字符串?爪哇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆