Results 1 to 3 of 3
- 12-17-2010, 06:53 PM #1
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
- 12-17-2010, 07:03 PM #2
the length considers all characters in the string. \n is a character, it just happens to be not printable and have special meaning to mean end of line
if you wanted to get the length excluding the \n, you could do
Where trim removes leading and following whitespace characters, which would also include spaces and tabs.Java Code:String a = "37107287\n"; System.out.println(a.trim().length());
Or in a more general case, if the string contains \n characters within it, or just exclude \n characters, use replaceAll to remove \n characters then take the length,
where we need to escape \ in a string.Java Code:System.out.println(a.replaceAll("\\n","").length());
[/code]
- 12-17-2010, 07:04 PM #3
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Similar Threads
-
String.length?
By rizowski in forum New To JavaReplies: 7Last Post: 11-20-2010, 05:33 PM -
string length HELP PLZ!!
By vette427 in forum EclipseReplies: 1Last Post: 09-23-2010, 06:12 AM -
Validating for length of String
By Bascotie in forum New To JavaReplies: 1Last Post: 10-11-2009, 11:45 AM -
Using PadRight but want to actually cut the string off at that length
By matpj in forum New To JavaReplies: 1Last Post: 01-14-2009, 05:19 PM -
[SOLVED] String Length issue.
By ChrisMayhew in forum New To JavaReplies: 7Last Post: 01-11-2009, 03:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks