Chapter 11: Strings


Section 11.3: Finding a String Within Another String


Download 0.83 Mb.
bet4/12
Sana18.12.2022
Hajmi0.83 Mb.
#1031312
1   2   3   4   5   6   7   8   9   ...   12
Bog'liq
dasturlsh string

Section 11.3: Finding a String Within Another String

String . o'z ichiga oladi


Muayyan a satri b qatorida mavjud yoki yo'qligini tekshirish uchun quyidagi sintaksis bilan () usulidan foydalanishimiz mumkin:

b.contains(a); // Return true if a is contained in b, false otherwise

String

.

contains

The () method can be used to verify if a CharSequence can be found in the String. The method looks for the String a in the String b in a case-sensitive way.

Live Demo on Ideone

String

.

indexOf

To find the exact position where a String starts within another String, use ():

String s = "this is a long sentence"; int i = s.indexOf('i'); // the first 'i' in String is at index 2 int j = s.indexOf("long"); // the index of the first occurrence of "long" in s is 10 int k = s.indexOf('z'); // k is -1 because 'z' was not found in String s int h = s.indexOf("LoNg"); // h is -1 because "LoNg" was not found in String s
Live Demo on Ideone

String

.

indexOf

String . o'z oladi
Muayyan a satri b qatorida mavjud yoki yo'qligini aniqlash uchun () usulidan sintaksis bilan borishimiz mumkin:
Note: The () method is case sensitive.
Example of search ignoring the case:

Live Demo on Ideone

Download 0.83 Mb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9   ...   12




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling