Solved

Very easy question Java String

Posted on 2014-01-01
3
348 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 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Jpanels and Jframe 8 33
Windows 10 and JDK 7 7u85 4 41
MySqlDump not dumping triggers 1 31
tomcat startup error 5 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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

770 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