An warware: tsaga abin da ya faru na farko

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.

Shafi posts:

Leave a Comment