Programming Articles - Page 3290 of 3363

How to count the number of words in a text file using Java?

Paul Richard
Updated on 20-Feb-2020 05:11:12

6K+ Views

Read the number of words in text fileCreate a FileInputStream object by passing the required file (object) as a parameter to its constructor.Read the contents of the file using the read() method into a byte array. Insatiate a String class by passing the byte array to its constructor.Using split() method read the words of the String to an array.Create an integer variable, initialize it with 0, int the for loop for each element of the string array increment the count.Exampleimport java.io.File; import java.io.FileInputStream; public class Sample {    public static void main(String args[]) throws Exception{       int count =0;       File ... Read More

How to count the number characters in a Java string?

Vikyath Ram
Updated on 20-Feb-2020 04:43:25

828 Views

Declare an integer, initialize it with 0, in for loop increment it for each character.ExampleLive Demopublic class Sample {    public static void main(String args[]) {       String str = new String("Hi welcome to Tutorialspoint");       int count = 0;       for(int i = 0; i

How to create a string from a Java ArrayList?

George John
Updated on 30-Jul-2019 22:30:20

14K+ Views

To convert the contents of an ArrayList to a String, create a StringBuffer object append the contents of the ArrayList to it, finally convert the StringBuffer object to String using the toString() method. Example import java.util.ArrayList; public class String_ArrayList { public static void main(String args[]) { ArrayList al = new ArrayList(); al.add("Hello"); al.add("are"); al.add("you"); StringBuffer sb = new StringBuffer(); for (String s : al) { sb.append(s); sb.append(" "); } String str = sb.toString(); System.out.println(str); } } Output Hello are you

How to create a string from a Java Array?

Arushi
Updated on 20-Feb-2020 04:49:12

446 Views

You can convert an array of Strings to a single array using the collect() method.ExampleLive Demoimport java.util.Arrays; import java.util.stream.Collectors; public class CreatngStringFromArray {    public static void main(String args[]) {       String [] myArray = {"Welcome", "to", "Tutorialspoint"};       String str = Arrays.stream(myArray).collect(Collectors.joining(" "));       System.out.println(str);    } }OutputWelcome to Tutorialspoint

How to extract certain substring from a string using Java?

Paul Richard
Updated on 20-Feb-2020 04:48:30

10K+ Views

You can extract a substring from a String using the substring() method of the String class to this method you need to pass the start and end indexes of the required substring.ExampleLive Demopublic class Substring {    public static void main(String args[]) {       String str = "Welcome to Tutorialspoint";       String sub = str.substring(10, 25);       System.out.println(sub);    } }OutputTutorialspoint

How to extract the last n characters from a string using Java?

Vikyath Ram
Updated on 20-Feb-2020 04:47:44

12K+ Views

To extract last n characters, simply print (length-n)th character to nth character using the charAt() method.ExampleLive Demopublic class ExtractingCharactersFromStrings {    public static void main(String args[]) {           String str = "Hi welcome to tutorialspoint";       int n = 5;       int initial = str.length()-5;       for(int i=initial; i

How to extract the first n characters from a string using Java?

Rishi Raj
Updated on 20-Feb-2020 04:46:52

916 Views

To find the consonants in the given String compare every character in it using the charAt() method with the vowel letters and remaining are consonants.ExampleLive Demopublic class FindingConsonants {    public static void main(String args[]) {       String str = new String("Hi Welcome to Tutorialspoint");       for(int i=0; i

How to find consonants in a given string using Java?

George John
Updated on 30-Jul-2019 22:30:20

4K+ Views

One way to find the vowels in a given String is to compare every character in it using the charAt() method with the vowel letters. Example Live Demo public class FindingConsonants { public static void main(String args[]) { String str = new String("Hi Welcome to Tutorialspoint"); for(int i=0; i

How to find the vowels in a given string using Java?

Arushi
Updated on 07-Oct-2023 02:01:20

38K+ Views

You can read a character in a String using the charAt() method. To find the vowels in a given String you need to compare every character in it with the vowel letters. Example Live Demo public class FindingVowels {    public static void main(String args[]) {       String str = new String("Hi Welcome to Tutorialspoint");       for(int i=0; i

How to search and replace text in a file using Python?

SaiKrishna Tavva
Updated on 05-Mar-2025 17:39:07

17K+ Views

File manipulation is a fundamental aspect of programming, especially when dealing with data processing and management. Python offers powerful tools for handling files and text efficiently. A common task is searching for specific text patterns within a file and replacing them with desired content. This article explores several practical methods for searching and replacing text in a file using Python. Basic Text Replacement Let's start with a simple example of searching for a specific word in a file and replacing it with another word. In this particular example, we'll search for the word "old" and replace it with "new" − ... Read More

Advertisements