Results 1 to 3 of 3
Thread: Frustrating simple problem.
- 09-15-2011, 08:14 PM #1
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
Frustrating simple problem.
Hi!
Heres the code:
As you can see, the string have 2 dots in it.PHP Code:public class PopupSample{ public static void main(String args[]){ String s = "int xcomp.XComponent.width"; System.out.println(s.split(".").length); } }
It will claim to be NO dots at all when you run this.
What is the problem?! Dot is not a special character that needs some sort of @£@$@£$\\@ . to be interpreted as a dot!
So why wont this simple split action work?
- 09-15-2011, 08:23 PM #2
Re: Frustrating simple problem.
you need to escape the period.
Java Code:System.out.println(s.split("\\.").length);
- 09-15-2011, 08:23 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,410
- Blog Entries
- 7
- Rep Power
- 17
Re: Frustrating simple problem.
A dot is special for the regular expression engine use by the split( ... ) method; a dot means 'any character' and you don't want that. The regular expression syntax wants you to escape the special meaning of that dot (meta) character so you should use "\\." instead of "." the double backslash is needed to keep Javac happy.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Simple but frustrating error
By JoeHill in forum New To JavaReplies: 10Last Post: 06-02-2011, 09:28 AM -
very frustrating.. recursive
By Yakg in forum New To JavaReplies: 5Last Post: 01-06-2011, 10:25 PM -
get index from an array (very frustrating)
By Yakg in forum New To JavaReplies: 4Last Post: 12-06-2010, 06:26 PM -
Please help setting up Tomcat. Very frustrating.
By wombatvvv in forum JavaServer Pages (JSP) and JSTLReplies: 5Last Post: 09-12-2010, 05:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks