Solved

Very easy question Java String

Posted on 2014-01-01
3
359 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39750313
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
ID: 39750327
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 75

Accepted Solution

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 240
runtime exception 2 50
Tagging and Merging on Branch 1 42
Java array 10 63
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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:

733 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