Java harshe ne na shirye-shirye iri-iri wanda ke da alaฦa da abu kuma ana amfani da shi sosai don shirye-shirye a duk faษin duniya. Yana da cikakken dandali, yana ba da babban ษakin karatu na kayan aikin da aka riga aka yi rajista, kamar waษanda ke taimakawa tare da magudin kirtani. A cikin wannan labarin, za mu duba cikin irin wannan yanayin guda ษaya, inda ake buฦatar mu *cire harafin farko daga kirtani**. Wannan aiki ne na gama gari a cikin ayyukan sarrafa rubutu kuma ana iya samunsa a Java ta hanyoyi da yawa. Bari mu bincika yadda za a iya yi.
String str = "Hello World"; String newStr = str.substring(1); System.out.println(newStr);
Za mu iya fahimtar wannan lambar a cikin matakai masu zuwa. Da farko, muna ayyana kuma mu fara ฦirฦira madaidaicin kirtani mai suna โstrโ tare da ฦimar โHello Duniyaโ. Ajin String a Java, wani yanki na kunshin java.lang, ana amfani da shi don ฦirฦira da sarrafa igiyoyin. Bayan haka, muna amfani da hanyar 'substring' na ajin String don ฦirฦirar sabon kirtani "newStr" da cire farkon halayen "str". A ฦarshe, muna buga sabon kirtani ta amfani da hanyar `System.out.println()`.
Hanyar substring a Java
Hanyar 'substring' wani bangare ne na **Kirtani a cikin Java**. Ainihin, ana amfani da shi don cire jerin haruffa daga kirtani. Hanyar tana da yawa kuma ta zo ta hanyoyi biyu, `substring(int beginIndex)` da `substring(int beginIndex, int endIndex)`.
A cikin wannan yanayin, muna amfani da nau'i na 'substring(int beginIndex)', inda kawai yake ษauka a cikin ma'auni wanda ke nuna ma'anar ma'anar da substring ya fara. Java yana amfani da sifili-based indexing, wanda ke nuna cewa fihirisar haruffan farko shine 0. Don haka, idan muna son cire harafin farko, zamu wuce 1 a matsayin ma'auni zuwa hanyar 'substring'. Wannan ya tsallake sifa ta farko kuma ya ฦirฦiri ฦaramin igiya daga hali na biyu har zuwa ฦarshe.
Madadin hanyoyin cire harafin farko
Ko da yake hanyar 'substring' ita ce hanya mafi gama gari da inganci don cire haruffan farko daga kirtani, akwai wasu hanyoyin da ake da su don cimma irin wannan.
ฦayan irin wannan hanyar ita ce ta amfani da ajin 'StringBuilder':
StringBuilder sb = new StringBuilder(str); sb.deleteCharAt(0); String newStr = sb.toString(); System.out.println(newStr);
A cikin wannan hanyar, mun fara fara wani abu na 'StringBuilder' tare da asalin kirtani. 'StringBuilder' madadin Class Class kuma lokacin da aka gyara kirtani ta amfani da shi, baya haifar da ฦirฦirar sabon misali. Sai mu yi amfani da hanyar `deleteCharAt` wanda ke share haruffa a ฦayyadaddun fihirisar. Bayan share harafin farko, za mu mayar da StringBuilder zuwa Kirtani ta amfani da `toString()' kuma mu buga sakamakon.
Hanyar cire haruffan farko daga kirtani ya dogara ne akan buฦatun shirin ku. Fahimtar ษakunan karatu da ayyuka suna da mahimmanci don yin zaษi mafi kyau. A cikin mafi girman hangen nesa, yana da ban sha'awa sosai don lura da yadda ainihin ma'anar kirtani zai iya haifar da ingantacciyar mafita ta shirye-shirye.