forked from mouredev/hello-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringExercises.java
More file actions
67 lines (41 loc) · 1.96 KB
/
Copy pathStringExercises.java
File metadata and controls
67 lines (41 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
public class StringExercises {
public static void main (String []args ) {
// 1. Concatena dos cadenas de texto.
var greet = ("Buenas tardes");
var name =("Jose");
System.out.println(greet + name);
// 2. Muestra la longitud de una cadena de texto.
System.out.println(greet.length());
// 3. Muestra el primer y último carácter de un string.
var char1 = (name.charAt(0));
var char2 = (name.charAt(name.length()-1));
System.out.println(char1 + " " + char2);
// 4. Convierte a mayúsculas y minúsculas un string.
System.out.println(greet.toUpperCase());
System.out.println(greet.toLowerCase());
// 5. Comprueba si una cadena de texto contiene una palabra concreta.
System.out.println(greet.contains("tard"));
// 6. Formatea un string con un entero.
int followers = 4200;
String nickname = "Hector Varon";
String cardNumber = "1234567";
int number;
number = Integer.parseInt(cardNumber);
System.out.println(number);
System.out.println(String.format("Mi alias es %s y tengo %d seguidores", nickname, followers));
// 7. Elimina los espacios en blanco al principio y final de un string.
var phrase =" No tengas miedo de fallar, ten miedo de no intentarlo ";
System.out.println(phrase);
System.out.println(phrase.trim());
// 8. Sustituye todos los espacios en blanco de un string por un guión (-).
System.out.println(phrase.replace(" ", "-"));
// 9. Comprueba si dos strings son iguales.
var country = "colombia";
System.out.println(country.equals("COLOMBIA"));
// 10. Comprueba si dos strings tienen la misma longitud.
var text1 = "pais";
var text2 = "ciuda";
boolean equaLength1 = (text1.length() == text2.length());
System.out.println("text1 == text2: " + equaLength1);
}
}