Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 409
  • 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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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