• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 433
  • Last Modified:

Very easy question Java String

Why use following?

String[] temp = input.split("\\s");

compared to

String[] temp = input.split(" ");

What is the reason to use \\s instead of a space?

Thanks
0
Ashok
Asked:
Ashok
  • 2
1 Solution
 
käµfm³d 👽Commented:
Because "\\s" is a regular expression**, and in this case it means "any whitespace", including spaces, tabs, newlines, carriage returns, and vertical tabs. What the split call splits on is is different between the two calls.

** Really, a single space is itself a regular expression. It's just easier for newbies to regular expressions to think of it as a literal string than a regular expression.
0
 
AshokAuthor Commented:
so if I use " ", it will not detect tab?

If I know for the fact that there is only one space is used to separate each word, which one should I use?
0
 
käµfm³d 👽Commented:
so if I use " ", it will not detect tab?
No. Only space.

If I know for the fact that there is only one space is used to separate each word, which one should I use?
You could stick with space.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now