0% found this document useful (0 votes)
34 views5 pages

String Text Processing

This document discusses various string processing methods in Java, including: 1. The concat() method concatenates two strings. The substring() method extracts a portion of a string between given indices. 2. The indexOf() and lastIndexOf() methods return the index of the first or last occurrence of a substring, or -1 if not found. 3. The replace() method replaces all instances of a substring in a string. StringBuilder allows efficient appending, insertion, deletion of strings.

Uploaded by

Bernal Familia
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
34 views5 pages

String Text Processing

This document discusses various string processing methods in Java, including: 1. The concat() method concatenates two strings. The substring() method extracts a portion of a string between given indices. 2. The indexOf() and lastIndexOf() methods return the index of the first or last occurrence of a substring, or -1 if not found. 3. The replace() method replaces all instances of a substring in a string. StringBuilder allows efficient appending, insertion, deletion of strings.

Uploaded by

Bernal Familia
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

TEXT PROCESSING

String hello = "Hello";


String brother = "brother!";

String result = hello.concat(brother); // Hello brother!


concat() – concatenaciqta suedinqva 2 stringa 

substring – vrushta stoinosta


String game = "AQWasda";
String nameGame = game.substring(0, 3);
System.out.println(nameGame); // AQW
v tozi sluchai izbirame ot koi index da vzemem imeto do tozi koito
sme mu zadali – 1, demek AQW (0, 3 - 1)
moje I substring (index start) koeto shte reche ot tozi index do
kraq

String text = "My name is John";


String extract = text.substring(11);
System.out.println("His name is " + extract); // John
v tozi sluchai sme mu izbrali index i toi vzima texta ot tozi index
do kraq na stringa

indexOf/lastIndexOf /SB
String fruits = "banana apple kiwi banana";

System.out.println(fruits.indexOf("banana")); // 0
System.out.println(fruits.indexOf("orange")); // -1

System.out.println(fruits.lastIndexOf("banana")); // 18
System.out.println(fruits.lastIndexOf("orange")); // -1
indexOf – pokazva na koi index se sreshta za pruv put ako nqma
takava duma printva -1
lastIndexOf – pokazva na koi index za posledno se poqvqva
dumata ako nqma takava printva -1
replace – zamestva dadenata duma(ako e ima) s tova koeto
iskame
String word = scanner.nextLine(); // pie

String fruits = "banana pie for you";

System.out.println(fruits.replace(word, "")); // banana for you

insert – insert vuvejda texta na dadeniq index


StringBuilder text = new StringBuilder();
text.append("Hello Peter, how are you?");

text.insert(11, " Ivanov");


System.out.println(text);

split – by multiple separators


String text = "Hello, I am John.";

String[] words = text.split("[, .]+"); // “Hello” ”I” “am” “John”


Moje da mu zadadem [ ] I v skobite po kakvoto da splitva
Koeto znachi ako vidish zapetaika splitvai i ako vidish space
splitvai i ako vidish tochka pak splitva
StringBuilder
String[] words = scanner.nextLine().split(" ");

StringBuilder result = new StringBuilder();

for (String word : words) {


int count = word.length();
for (int i = 0; i < count; i++) {
result.append(word);
}
}

za vsqka duma ot masiva vzemi word.length int count, I za length


dobavqi(append) tolkova puti dumata kolkoto e word.length
StringBuilder – sushto zadelq malko poveche pamet za da moje
kogato imame da dobavim nqkakuv string kum masivcheto da ne
izvurshva mnogo tejki operacii
StringBuildera – moje da go .toString
delete(SB) –
StringBuilder numbers = new StringBuilder();
numbers.append(scanner.nextLine()); // 123456789

numbers.delete(0, 5);
System.out.println(numbers); // 6789

replace(SB) – drugiq replace e che mu zadavame index ot


diapazon (int start – int end - 1) v koito mu kazvame mahni texta v
tozi diapazon i go zameni s noviq text
StringBuilder text = new StringBuilder();
text.append("Hello dude, how are you?");

System.out.println(text.replace(6, 10, "bro"));


System.out.println(text);
Character – METHODS
Character.(ima mnogo metodi koito proverqvat dali e chislo, letter
etc…)
StringBuilder numbers = new StringBuilder();
StringBuilder letters = new StringBuilder();
StringBuilder symbols = new StringBuilder();

String text = scanner.nextLine(); // 22aA%%

for (int i = 0; i < text.length(); i++) {


char token = text.charAt(i);

if (Character.isDigit(token)) {
numbers.append(token);
} else if (Character.isLetter(token)) {
letters.append(token);
} else {
symbols.append(token);
}
}

System.out.println(numbers); // 22
System.out.println(letters); // aA
System.out.println(symbols); // %%

Tova e zadacha v koqto proverqvame I razdelqme texta na chisla


bukvi I symbols

You might also like