each word in a string.
For instance:
code: string = "How you guys doing";
out.println(string);
output: "How" "you" "guys" "doing"
I thought about putting my code in a String and making a method that puts a quotation mark every time it sees a space. But I don't really know how to make such method. Is there any library for this? Any other idea?
Thanks.
Have you tried regex and groups ?
nope didn't hear about them yet. I Googled them, and they seems to fit my need. Thanks!
Regex is probably the easiest. But to learn more, here is a alternative to try out: -> Split the string -> Use Collectors.joining() and a stream() where you first use a map() to manipulate each string and add the ".
classic regex (through pattern, matcher, groups, etc) would be definitely an overkill here. and based on the task and your thoughts about it, I assume that you're at the beginning of your road of learning java. and that's more important reason to not dive into regex. for now. I suggest you to conentrate on java's main features, fundamental ones like loops, flow control constructs, etc., OOP, main sdk classes. and only after that (or even later) dive into regular expressions. just don't switch your focus to regular expressions, concentrate on fundamental stuff. for this task it's enough 3 simple things: - String.split(String regex) method which allows split one string into pieces by provided delimiter. yeah, it actually accepts regex, but for simple cases like that it works same as delimiter, so in your case delimiter is a single space string - " " - for loop or foreach loop - and + operator that works on strings As an example (I assume that you're already familiar with arrays): String text = "several__words__go__here"; String delimiter = "__"; String[] words = text.split(delimiter); // voila, you have array of words of the original text String word = "one"; String modified = "prefix-" + word + "-suffix"; Hm... maybe this example is too obvious. Hope it will be more helpful than harmful. Here's first google result with examples for String.split() method: https://www.baeldung.com/string/split
Thank you!!! I really appreciate this so much. And yes, I'm still at the beginning, I started learning GUI only 2 days ago.
Обсуждают сегодня