Rarraba farkon abin da ya faru na harafi ko kirtani a cikin rubutu aiki ne na gama gari yayin da ake mu'amala da ayyukan sarrafa bayanai da sarrafa bayanai. A cikin Java, ana iya samun wannan cikin sauฦi tare da ginanniyar hanyoyin. A yau, za mu bi ta kowane mataki da ke da hannu wajen rarrabuwar farkon abin da ya faru a cikin zaren, mu rarraba lambar da ke ciki, da bincika abubuwan da ke da alaฦa da makamantan abubuwan da za ku iya fuskanta.
Rarraba abin da ya faru na farko: Magani
Don raba farkon abin da ya faru na Sting ko hali a Java, muna amfani da ginanniyar ayyuka da yawa. Hanya mai mahimmanci don cimma wannan ita ce amfani da indexOf() da kuma substring() hanyoyin. Ga snippet mai sauฦi wanda ke nuna wannan:
public class Main { public static void main(String[] args) { String str = "Hello-World-This-is-Java"; int index = str.indexOf('-'); String firstPart = str.substring(0, index); String secondPart = str.substring(index + 1); System.out.println(firstPart); System.out.println(secondPart); } }
Bayanin mataki-mataki na lambar
1. String str = "Sannu-Duniya-Wannan-Java";
A cikin wannan layin, muna fara strring variable str wanda ya ฦunshi kirtani da muke son raba.
2. int index = str.indexOf ('-');
Yin amfani da hanyar indexOf(), muna samun fihirisar farkon abin da ya faru na harafin ''-'. Lura cewa idan har ba a sami halayen indexOf() ba zai dawo -1.
3. Sashe na farko = str.substring(0, index);
Muna amfani da hanyar substring don samun rabin farko na String, daga farkon kirtani har zuwa farkon abin da ya faru na ''-'. An sanya wannan ga mai canzawa kashi na farko.
4. Zaure na biyuPart = str.substring(index + 1);
A ฦarshe, muna sake amfani da hanyar substring don samun ragowar ษangaren kirtani, daga bayan farkon abin da ya faru na ''-' har zuwa ฦarshe. An sanya wannan zuwa Sashe na biyu.
Hanyoyin indexOf() da substring() hanyoyin
The indexOf() Hanyar wani bangare ne na java.lang.String class. Yana mayar da matsayin farkon abin da ya faru na ฦayyadadden hali/s a cikin kirtani. Duk madaidaicin kirtani a cikin shirye-shiryen Java, kamar "abc", ana aiwatar da su azaman misalin wannan ajin.
The substring() Hanyar wani bangare ne na aji daya. Hanyar substring tana dawo da sabon kirtani wanda shine juzu'in kirtani da aka bayar. Wannan hanyar tana da yawa fiye da kima kuma tana iya ษaukar sigogi ษaya ko biyu - fihirisar farawa, da zaษin ฦarshen fihirisar. Idan ba a kayyade fihirisar ฦarewa ba, za ta cire har zuwa ฦarshen kirtani.
Java kuma yana ba da wasu ayyuka da yawa masu alaฦa da Strings a daidaitaccen ษakin karatu, yana mai da shi yare mai dacewa don ayyukan sarrafa bayanai. Hanyoyin da aka nuna a sama biyu ne kawai a cikin yawancin da za ku iya amfani da su don yin hulษa tare da kirtani da haruffa a cikin shirin Java.
Ka tuna cewa tare da kyakkyawar fahimtar waษannan ra'ayoyin, za ku iya magance matsalar ba kawai wannan matsala ba amma har ma da sauran ayyukan sarrafa bayanai iri ษaya. Java kayan aiki ne mai ฦarfi a hannun dama.
Makamantan Shari'o'i da Sauran Hanyoyi Masu Amfani na Java
Baya ga indexOf() da kuma substring() hanyoyin, Java yana ba da wasu ginanniyar hanyoyin da aka gina don gudanar da irin waษannan lokuta. Misali, da char() hanyar da ke dawo da halin da ke a ฦayyadadden fihirisar, ko kuma tsaga () hanyar da ke raba kirtani a kusa da matches na magana ta yau da kullum.
//Example of charAt() method String str = "Hello World"; char result = str.charAt(7); System.out.println(result); // This will output 'o' //Example of split() method String[] parts = str.split(" "); String part1 = parts[0]; // "Hello" String part2 = parts[1]; // "World"
Ajin String na Java yana da wadatuwar fasali kuma ya fi isa ga yawancin ayyukan sarrafa kirtani. Ta hanyar fahimtar waษannan hanyoyin, za ku sami kyakkyawan umarni kan sarrafa kirtani a Java.