An warware: cire harafin farko daga kirtani

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.

Shafi posts:

Leave a Comment