Sie sind auf Seite 1von 2

2/14/14

How to get line count from a string? - Java Sample Programs

JAVA EXAMPLE PROGRAMS


0

Home Fundamentals Constructors Exception Handling Threads String Functions Generics Collections & Util Package Nested Classes Networking File I/O Operations Java Annotations JDBC Examples Java Interview Questions Java Interview Programs JUnit Search Algorithms Sorting Algorithms Data Structures JBoss Configurations

Program: How to get line count from a string?


Description: 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.

Code: 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 p a c k a g ec o m . j a v a 2 n o v i c e . s t r i n g ; p u b l i cc l a s sM y S t r i n g L i n e C o u n t e r{ p u b l i cs t a t i ci n tg e t L i n e C o u n t ( S t r i n gt e x t ) { } r e t u r nt e x t . s p l i t ( " [ \ n | \ r ] " ) . l e n g t h ; Knowledge Center Domain Naming Service(DNS)


?

p u b l i cs t a t i cv o i dm a i n ( S t r i n ga [ ] ) { S t r i n gs t r=" l i n e 1 \ n l i n e 2 \ n l i n e 3 \ r l i n e 4 " ; S y s t e m . o u t . p r i n t l n ( s t r ) ; i n tc o u n t=g e t L i n e C o u n t ( s t r ) ; S y s t e m . o u t . p r i n t l n ( " l i n ec o u n t :" + c o u n t ) ;

Output: line1 line2 line3 line4 line count: 4

It is very difficult to remember a s numbers (IP address) to connect to Internet. The Domain Naming Serv used to overcome this problem. It particular IP address to a string of For example, www.java2novice.co com is the domain name reserved commercial sites, java2novice is t the company and www is the name specific computer, which is java2n server.

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 Programs


Like 1,608 people like Java Sample Programs

Facebook social plugin

www.java2novice.com/java_string_examples/line-count/

1/2

2/14/14

How to get line count from a string? - Java Sample Programs


Few Random Java Examples

What is generics wildcard argumen example. Write a program to find maximum words from a file.

Find out duplicate number between numbers.

Can you serialize static fields of a c

Placement Java Interview Question

How to convert byte array to inputs

How to get subset from sorted set?

How implement bounded types (imp interface) with generics? How to zip a single file?

About Author
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.

Most Visited Pages


Freshers Java Interview Questions How to Create Java Custom Exception How to convert byte array to inputstream? Java StringTokenizer With Multiple De-limiters Sample Code Java Constructor Chaining Examples Write a program to find out duplicate characters in a string. Write a program to find maximum repeated words from a file. How to read file content using byte array?

Other Interesting Sites


Techie Park Wikipedia query2nataraj.blogspot.in

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

www.java2novice.com/java_string_examples/line-count/

2/2

Das könnte Ihnen auch gefallen