Sei sulla pagina 1di 2

2/14/14

Howto get line count from a string? - Java Sample Programs

JAVA EXAMPLE PROGRAMS
JAVA EXAMPLE PROGRAMS
0 Home Program: How to get line count from a string? Fundamentals Description: Constructors Exception
0
Home
Program: How to get line count from a string?
Fundamentals
Description:
Constructors
Exception Handling
Threads
This example shows how to get line count from a string. Assuming that we have read the file and keeping the
content in string. We are using String.split() method with the use of regular expression [\n|\r]. It will split the
string based on the new line char and carriage return char. After the split, we will get string array, and returning
length of the array.
String Functions
Generics
Code:
Collections & Util Package
1
packagecom.java2novice.string;
?
2
Nested Classes
3
publicclassMyStringLineCounter{
4
Knowledge Center
5
publicstaticintgetLineCount(Stringtext){
Networking
6
Domain Naming Service(D
7
returntext.split("[\n|\r]").length;
File I/O Operations
8
}
9
Java Annotations
10
publicstaticvoidmain(Stringa[]){
11
12
Stringstr="line1\nline2\nline3\rline4";
JDBC Examples
13
System.out.println(str);
14
intcount=getLineCount(str);
Java Interview Questions
15
System.out.println("linecount:"+count);
16
}
Java Interview Programs
17
}
JUnit
Output:
Search Algorithms
It is very difficult to rem
numbers (IP address) to
Internet. The Domain Na
used to overcome this pr
particular IP address to a
For example, www.java2
com is the domain name
commercial sites, java2n
the company and www is
specific computer, which
server.
Sorting Algorithms
line1
line2
Data Structures
line3
line4
JBoss Configurations
line count: 4
Other String Function Examples
String Initialization example
Copying charactor array to string example
String concatination example
String compare example
Comparing StringBuffer with a String example
Getting byte array from a string example
String indexOf() example
String lastIndexOf() example
String startsWith() example
String endsWith() example
String split function example
Extract char array from string example
Replace characters in a string example
Changing string case example
Trim spaces in string example
How to format a string?
How to match a string with regular expression?
How to remove multiple space in a string?
How to remove non-ascii characters from a string?
How to remove html tags from a string?
Find us on Facebook
Java Sample P
LLiikkee
1,608 people like Java Sample
Facebook social plugin

2/14/14

Howto get line count from a string? - Java Sample Programs

Few Random Java Examp What is generics wildcard example. Write a program to find words
Few Random Java Examp
What is generics wildcard
example.
Write a program to find
words from a file.
Find out duplicate numbe
numbers.
Can you serialize static fi
Placement Java Interview
How to convert byte arra
How to get subset from s
How implement bounded
interface) with generics?
How to zip a single file?

About Author

Most Visited Pages

Other Interesting Sites

My name is Nataraja Gootooru, programmer by profession and

passionate about technologies. All examples given here are as

simple as possible to help beginners. The source code is compiled

and tested in my dev environment.

If you come across any mistakes or bugs, please email me at

java2novice@gmail.com or you can comment on the page.

Reference: Java™ Platform Standard Ed. 6 - API Specification | Java is registered trademark of Oracle. Privacy Policy | Copyright © 2013 by Nataraja Gootooru. All Rights Reserved.