java


I am a bit confused as to what the code below is doing since I get the same results from both


String[] words = numOfWords.split("\\s+");
String[] words = numOfWords.trim().split("\\s+");
String.trim() only removes leading and trailing white space characters. Unless numOfWords starts or ends with white space, I would expect them to be the same. For an example where the results differ, you could do something like
String[] words1 = " a b".split("\\s+");
String[] words2 = " a b".trim().split("\\s+");
System.out.printf("%s %s", Arrays.toString(words1), Arrays.toString(words2));
Which outputs
[, a, b] [a, b]
.split will do something like "you, are , a, developer"
And the top line is doing that and using Regex to tell where to split the string.
The second line of code is doing the same thing with redundancy .

Related Links

Cant generate java code from wsdl file
Jar with multiple main
EasyMock - mock object returned from new Object
Mockito: how to stub inherited methods
Java Making a Custom Report PDF with iText
Calculations between two integer arrays
how to add utf8 text to image with diffrent ttf fonts?
How is it getting an instantiation error and how to fix it? [Android]
Locations saved in sqlite not shown in list [duplicate]
Array list iterators next and previous not working
Is there a way to control windows in another Java process?
JBoss Singleton Service Example: Does not expose a singleton object
YAML multiple keys with multiple values per element
NullPointerException while calling a method from another class [duplicate]
Cant control JavaFX TitledPane with FXML
how to define hashmap within hashmap using object of other hashmap

Categories

HOME
compiler-construction
listview
netbeans
openstack
iot
electron
google-oauth
elm
paw-app
c#-2.0
retrofit
alpha
ezpublish
convolution
networkx
directx
append
basic
gitpitch
iggrid
gnupg
quicklook
qt-creator
vb.net-2010
circuit
lombok
ef-migrations
propel
crystal-reports-2008
viewport
tapestry
realex-payments-api
microsoft-sync-framework
qhull
status
wtx
atlassian-crucible
instant-messaging
bosh
galsim
airconsole
code-contracts
kendo-ui-grid
vsts-build-task
openoffice.org
wallpaper
y86
google-api-nodejs-client
android-ble
android-fingerprint-api
sfdc
ruby-on-rails-3.1
ibpy
mime
import-from-excel
gpx
eclipse-gef
businessworks
logfiles
fault
dotcover
chain-builder
strptime
dds
alphabet
android-cursor
nsarray
memory-alignment
phishing
jwplayer7
angstrom-linux
qgraphicsview
nessus
ptrace
two-factor-authentication
energy
collapse
comobject
simple-framework
django-unittest
service-accounts
processmodel
p2
notify
jsapi
wordpress-theme-customize
xcode-6.2
issuu
internet-connection
code-access-security
htmlcleaner
dealloc
bundles
mcts
runtime.exec
bulkloader
pvrtc
word-processor
armcc
labwindows
jspinclude
buildr
cufon
genshi
xmemcached
locate
h.323
forums
mirah
substrings
sef
putchar
ubuntu-9.04

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App