Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

2.4 Οι συμβολοσειρές (strings) στην Java

Οι συμβολοσειρές (strings) χρησιμοποιούνται για την αποθήκευση κειμένου.

Μια μεταβλητή String περιέχει μια συλλογή χαρακτήρων που περιβάλλονται από διπλά εισαγωγικά:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String greeting = "Hello World";
System.out.println(greeting);
String greeting = "Hello World"; System.out.println(greeting);
String greeting = "Hello World";
System.out.println(greeting);

Αν τρέξετε τον παραπάνω κώδικα, θα εμφανιστεί στην οθόνη το κείμενο "Hello World".

Μπορείτε επίσης να συνδυάσετε δύο συμβολοσειρές χρησιμοποιώντας τον τελεστή +:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName;
System.out.println(fullName);
String firstName = "John"; String lastName = "Doe"; String fullName = firstName + " " + lastName; System.out.println(fullName);
String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName;
System.out.println(fullName);

Αν τρέξετε τον παραπάνω κώδικα, θα εμφανιστεί στην οθόνη το κείμενο "John Doe".

Μια συμβολοσειρά στην Java είναι στην πραγματικότητα ένα αντικείμενο που περιέχει μεθόδους που μπορούν να εκτελέσουν κάποιες λειτουργίες πάνω στις συμβολοσειρές. Για παράδειγμα, η μήκος μιας συμβολοσειράς μπορεί να βρεθεί με τη μέθοδο length():

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String text = "Hello World";
int length = text.length();
System.out.println(length);
String text = "Hello World"; int length = text.length(); System.out.println(length);
String text = "Hello World";
int length = text.length();
System.out.println(length);

Αν τρέξετε τον παραπάνω κώδικα, θα εμφανιστεί στην οθόνη ο αριθμός 11, η οποία είναι η μήκος της συμβολοσειράς "Hello World".

Αναφέροντας το παράδειγμα που αναφέρθηκε παραπάνω, μπορούμε να το γράψουμε σε μορφή κώδικα με τη main() μέθοδο ως εξής:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class StringLengthExample {
public static void main(String[] args) {
String text = "Hello World";
int length = text.length();
System.out.println(length);
}
}
public class StringLengthExample { public static void main(String[] args) { String text = "Hello World"; int length = text.length(); System.out.println(length); } }
public class StringLengthExample {
    public static void main(String[] args) {
        String text = "Hello World";
        int length = text.length();
        System.out.println(length);
    }
}

Αν τρέξουμε τον κώδικα, θα εμφανιστεί στην οθόνη ο αριθμός 11.

Υπάρχουν πολλές μέθοδοι συμβολοσειράς στη διάθεσή μας στην Java, όπως οι μέθοδοι toUpperCase() και toLowerCase() που χρησιμοποιούνται για την μετατροπή των χαρακτήρων της συμβολοσειράς σε κεφαλαία και πεζά γράμματα αντίστοιχα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String text = "Hello World";
String upperCaseText = text.toUpperCase();
String lowerCaseText = text.toLowerCase();
System.out.println(upperCaseText); // Εμφανίζει "HELLO WORLD"
System.out.println(lowerCaseText); // Εμφανίζει "hello world"
String text = "Hello World"; String upperCaseText = text.toUpperCase(); String lowerCaseText = text.toLowerCase(); System.out.println(upperCaseText); // Εμφανίζει "HELLO WORLD" System.out.println(lowerCaseText); // Εμφανίζει "hello world"
String text = "Hello World";
String upperCaseText = text.toUpperCase();
String lowerCaseText = text.toLowerCase();
System.out.println(upperCaseText); // Εμφανίζει "HELLO WORLD"
System.out.println(lowerCaseText); // Εμφανίζει "hello world"

Οι παραπάνω μέθοδοι δεν αλλάζουν την αρχική συμβολοσειρά, αλλά δημιουργούν μια νέα συμβολοσειρά με τα αντίστοιχα κεφαλαία ή πεζά γράμματα.

Η μέθοδος indexOf() επιστρέφει τη θέση της πρώτης εμφάνισης μιας συγκεκριμένης υποσυμβολοσειράς ή χαρακτήρα στην αρχική συμβολοσειρά. Αν η υποσυμβολοσειρά ή ο χαρακτήρας δεν βρεθεί, επιστρέφει την τιμή -1.

[adinserter block=”2″]

Η σύνταξη της indexOf() είναι η εξής:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
int indexOf(String textToFind)
int indexOf(String textToFind)
int indexOf(String textToFind)

Παρακάτω παρατίθενται παραδείγματα χρήσης της indexOf():

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String text = "The quick brown fox jumps over the lazy dog";
int position1 = text.indexOf("fox"); // Επιστρέφει 16
int position2 = text.indexOf("dog"); // Επιστρέφει 40
int position3 = text.indexOf("cat"); // Επιστρέφει -1
String text = "The quick brown fox jumps over the lazy dog"; int position1 = text.indexOf("fox"); // Επιστρέφει 16 int position2 = text.indexOf("dog"); // Επιστρέφει 40 int position3 = text.indexOf("cat"); // Επιστρέφει -1
String text = "The quick brown fox jumps over the lazy dog";
int position1 = text.indexOf("fox"); // Επιστρέφει 16
int position2 = text.indexOf("dog"); // Επιστρέφει 40
int position3 = text.indexOf("cat"); // Επιστρέφει -1

Στο παραπάνω παράδειγμα, η indexOf() επιστρέφει τη θέση της πρώτης εμφάνισης των υποσυμβολοσειρών "fox" και "dog" στην αρχική συμβολοσειρά. Επίσης, η indexOf() επιστρέφει την τιμή -1, καθώς η υποσυμβολοσειρά "cat" δεν υπάρχει στην αρχική συμβολοσειρά.

Στην Java οι θέσεις (positions) στα strings αριθμούνται από το μηδέν. Άρα η πρώτη θέση έχει δείκτη 0, η δεύτερη έχει δείκτη 1, η τρίτη έχει δείκτη 2 κ.ο.κ.

Ο συντελεστής + μπορεί να χρησιμοποιηθεί μεταξύ συμβολοσειρών για να τις συνδέσει. Αυτό ονομάζεται συνένωση συμβολοσειρών:

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName;
System.out.println(fullName);
String firstName = "John"; String lastName = "Doe"; String fullName = firstName + " " + lastName; System.out.println(fullName);
String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName;
System.out.println(fullName);

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
John Doe
John Doe
John Doe

Μπορείτε επίσης να χρησιμοποιήσετε τη μέθοδο concat() για να συνδέσετε δύο συμβολοσειρές:

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String firstName = "John";
String lastName = "Doe";
String fullName = firstName.concat(" ").concat(lastName);
System.out.println(fullName);
String firstName = "John"; String lastName = "Doe"; String fullName = firstName.concat(" ").concat(lastName); System.out.println(fullName);
String firstName = "John";
String lastName = "Doe";
String fullName = firstName.concat(" ").concat(lastName);
System.out.println(fullName);

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
John Doe
John Doe
John Doe

ΠΡΟΣΟΧΗ!

Η Java χρησιμοποιεί τον συντελεστή + τόσο για πρόσθεση όσο και για συνένωση.

Οι αριθμοί προστίθενται. Οι συμβολοσειρές συνενώνονται.

Εάν προσθέσετε δύο αριθμούς, το αποτέλεσμα θα είναι ένας αριθμός:

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
int x = 10;
int y = 5;
int sum = x + y;
System.out.println(sum);
int x = 10; int y = 5; int sum = x + y; System.out.println(sum);
int x = 10;
int y = 5;
int sum = x + y;
System.out.println(sum);

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
15
15
15

Εάν συνδέσετε δύο συμβολοσειρές, το αποτέλεσμα θα είναι μια συμβολοσειρά που περιέχει τη συνένωσή τους:

[adinserter block=”3″]

Παράδειγμα με δύο συμβολοσειρές αριθμών:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String x = "10";
String y = "5";
String result = x + y;
System.out.println(result);
String x = "10"; String y = "5"; String result = x + y; System.out.println(result);
String x = "10";
String y = "5";
String result = x + y;
System.out.println(result);

Έξοδος:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
105
105
105

Εάν προσθέσετε έναν αριθμό και μια συμβολοσειρά, το αποτέλεσμα θα είναι μια συμβολοσειρά συνένωσης, καθώς η συμβολοσειρά “τραβάει” τον αριθμό σε μορφή συμβολοσειράς:

Παράδειγμα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
int x = 10;
String y = " apples";
String result = "I have " + x + y;
System.out.println(result);
int x = 10; String y = " apples"; String result = "I have " + x + y; System.out.println(result);
int x = 10;
String y = " apples";
String result = "I have " + x + y;
System.out.println(result);

Έξοδος:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
I have 10 apples
I have 10 apples
I have 10 apples

Διότι οι συμβολοσειρές πρέπει να γράφονται μέσα σε εισαγωγικά, το Java θα παρερμηνεύσει αυτή τη συμβολοσειρά και θα δημιουργήσει ένα σφάλμα. Για παράδειγμα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String txt = "We are the so-called "Vikings" from the north.";
String txt = "We are the so-called "Vikings" from the north.";
String txt = "We are the so-called "Vikings" from the north.";

Για να αποφύγετε το παραπάνω σφάλμα, μπορείτε να χρησιμοποιήσετε το χαρακτήρα backslash () για να δηλώσετε τα εισαγωγικά μέσα στη συμβολοσειρά:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
String txt = "We are the so-called \"Vikings\" from the north.";
String txt = "We are the so-called \"Vikings\" from the north.";
String txt = "We are the so-called \"Vikings\" from the north.";

Αυτό θα δημιουργήσει τη σωστή συμβολοσειρά και η έξοδος θα είναι:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
We are the so-called "Vikings" from the north.
We are the so-called "Vikings" from the north.
We are the so-called "Vikings" from the north.

Οι κοινές ακολουθίες διαφυγής χαρακτήρων που χρησιμοποιούνται στην Java είναι:

Κώδικας Αποτέλεσμα
\n Νέα Γραμμή
\r Επιστροφή Κέραιας (Carriage Return)
\t Εναλλαγή καρτελών (Tab)
\b Επιστροφή στην αρχή (Backspace)
\f Αλλαγή Σελίδας (Form Feed)

Μπορείτε να χρησιμοποιήσετε αυτούς τους κωδικούς στις συμβολοσειρές για να δημιουργήσετε ειδικούς χαρακτήρες. Παρακάτω είναι ένα παράδειγμα:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
System.out.println("Hello\nWorld!");
System.out.println("Hello\nWorld!");
System.out.println("Hello\nWorld!");

Εξοδος:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Hello
World!
Hello World!
Hello
World!

Δωρεα μεσω Paypal

Για την κάλυψη αναγκών φιλοξενίας και δημιουργίας περιεχομένου.

κατηγοριες μαθηματων

Ιστορικο ενοτητων

top
error: Content is protected !!
Μετάβαση σε γραμμή εργαλείων