Handling strings in Java - ProgrammerTech
Language:

Search

Handling strings in Java

  • Share this:
Handling strings in Java

Text data processing Strings

Text data ( strings ) is a string of characters entered by the user from the keyboard called strings. They are defined in the Java language as texts, and the String is everything that is entered from the keyboard and entered in the form of text strings. We will learn two things:

  1. How do we find the length of a text string in Java?
  2. How do we combine two strings into one string in Java?

 

Example 1

Write a program in Java that calculates the length of the number of characters of the word "saja" using the length function.

package str;
public class Str {
    public static void main(String[] args) {
        String s="saja ";
        System.out.println(s.length());
    }
}

We created a text variable named s and its value saja, and when we run this program, it will print the number 5 for us because we left a space at the end of the word and its length is considered to be five.

5

When the space is removed, 4 will be printed because the String considers the space as a character.

Example 2

Write a program using Java that combines two strings, the first CS and the second DB, using the concat function.

package str;
public class Str {
    public static void main(String[] args) {
        String s="CS";
        String s1="DB";
        s1=s1.concat(s);
        System.out.println(s.length(s1));
    }
}

We created the first variable named s and its value CS, and we created the second variable named s1 and its value DB, then we told it s1 equals s1 and we combined it with the variable s and we printed the result of s1. And when executing, CSDB will print for us, merging the two scripts together.

CSDB

 

Subtract part of a string

When we say strings, we mean a group of words that make up a text string or group. Letters form a text string or a group of symbols, letters and words. They all form a text string. We will learn how to subtract part of a string. We may deduct a letter, word, or sentence from a text string. In this command, we use the substring function, which subtracts part of the text strings.

example 3

Create two text variables, the first is saja and the second is Muhammad, then subtract parts of the text string through the substring function with one parameter.

package str;
public class Str {
    public static void main(String[] args) {
        String s1="saja";
        String s2="Muhammad";
        String s3=s1.substring(2);
        System.out.println(s.length(s3));
    }
}

We created a variable named s1 and its value saja, and we created a second variable named s2 and its value Muhammad. Then we created a third variable named s3 and stored the first variable in it and deducted the first two letters from it via the substring function. Then we printed the variable s3 and when executing it will print the word ja because it cut out or ignored the first two letters after we specified them to it.

ja

Example 4

Create two text variables, one with the value of saja and the second with the value of Muhammad, and then subtract a part. From the second variable from 1 to 4 of the text string via the two parameter substring function.

package str;
public class Str {
    public static void main(String[] args) {
        String s1="saja";
        String s2="Muhammad";
        String s3=s2.substring(1,4);
        System.out.println(s.length(s3));
    }
}

We created three variables, the first with the value saja and the second with the value Muhammad. Then we created a third variable, s3, and told him it was deducted from the variable s2 from 1 to 4, ie it starts from the first index and stops at the fourth index. When executing it will print uha for us and ignore the rest of the speech.

uha

 

charAt function in java

The charAt() function is one of the text functions that is used to manipulate strings, and it extracts a character by location, ie it returns a value of type char.

Example 5

Suppose we have a string which is kaissar, deduct a character from this string via the charAt function.

package str;
public class Str {
    public static void main(String[] args) {
        String s="kaisar";
        char c;
        c=s.charAt(4);
        System.out.println(c);
    }
}

We create a text variable, s, and its value is kaisar. Then we define a char variable, which is c. Then we store the output in c. We told him to print the index or the letter number 4 for us through the charAt function. And when executed, the result will be the letter a because its position is number 4 of this string.

a

Example 6

Find the number of spaces in a text string stored in a text variable s and its value "Muhammad Alush" Find the number of spaces in this string and print their number.

package str;
public class Str {
    public static void main(String[] args) {
        String s="Muhammad Alush";
        char c;
        int count=0;
        for(int i=0;i<s.length();i++)
        {
            c=s.charAt(i);
            if(c==' ')
                count++;
        }
        System.out.println(count);
    }
}

At the beginning of the program we defined a text variable s and its value was "Muhammad Alush" and we defined a variable whose type is char and its name is c. Then we created an integer variable named count and the value 0, then we entered a for loop and set conditions. In the event that there is a space, it provides us with the number of spaces it found and increases the count by 1. When the program is run, it will print the number 1 for us, and this means that in this string there is only one space, which is a separator between the name and the nickname.

 

Flexible dynamic text StringBuffer

Flexible, variable, or volatile text means a string that is of a type. StringBuffer allows us to change, modify and deal with it with ease and flexibility. And any string defined as StringBuffer gives us a class, which is basically a class that gives us a lot of functions for dealing with strings.

Example 7 The append add function

package str;
public class Str {
    public static void main(String[] args) {
        StringBuffer s=new StringBuffer("saja");
        s.append("veil");
        System.out.println(s);
    }
}

In this example, we created a variable s named StringBuffer and the value saja and then used the append function to add . word veil for this string and then we print the string and the result is sajaveil.

sajaveil

Example 8 Function Insert

package str;
public class Str {
    public static void main(String[] args) {
        StringBuffer s=new StringBuffer("saja");
        s.insert(0, "veil");
        System.out.println(s);
    }
}

In this example, we used the insert function to insert the word veil at the beginning of the text string saja and the number 0 means to insert the word veil at the beginning of the string. The result will be veilsaja, and if we write number 1, he will add it after the letter s of the name saja.

veilsaja

Example 9 The replace . function

package str;
public class Str {
    public static void main(String[] args) {
        StringBuffer s=new StringBuffer("Alsadeq");
        s.replace(0,2, "sajo");
        System.out.println(s);
    }
}

We have the string Alsadeq and we want to replace part of it via the replace function, we used the replace function. Then we set the two numbers 0,2, which means replace us from the index 0 to 2 with the word sajo, and upon execution, the result will be sajaadeq.

sajaadeq

Example 10 of the delete function

package str;
public class Str {
    public static void main(String[] args) {
        StringBuffer s=new StringBuffer("Alsadeq");
        s.delete(0, 1);
        System.out.println(s);
    }
}

In this example, we asked the program to delete from 0 to 1 from the text string specified by the function. delete then prints the string, and the result will be sadeq because it deleted the first two characters.

sadeq

Tags:
Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com