Solved

Very easy question Java String

Posted on 2014-01-01
3
339 Views
Last Modified: 2014-01-01
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
Comment
Question by:Ashok
  • 2
3 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
 
LVL 13

Author Comment

by:Ashok
Comment Utility
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
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now