techhipbettruvabetnorabahisbahis forumutaraftarium24eduseduseduedueduedusedueduseduedu Copying Character Array To String In Java - Tutorial
Latest :

Copying Character Array To String In Java – Tutorial

Java tutorial to copy character array to string. Here, we will discuss about the various methods of copying a character array to a string using. If you have any doubts leave a comment here at the end of the post.

  • Creating a string object based on the character array.
  • Adding character by character to an initially empty string.
  • Setting character by character into a string buffer.
  • Using the static method CopyValueOf() of string class.
  • Using the static method ValueOf() of string class.

Creating a String object based on the character array

This is the simplest way of copying a character array into a String.

For Example:

Output:

In the above example, we have an array of 5 characters (containing the letters h, i, m, m and u). Length of this array is 5 (as we have 5 characters).

Then we have created a String object by specifying the character array as argument to constructor. With this a new String is created with the characters of the character array. It means the new String will be of length 5 with the characters h,i,m,m and u at 0,1,2,3 and 4 locations respectively. The output confirms it.

 Adding character by character to an (initially) empty string

Sample logic:

Output:

In this example, we have an empty string initially. And then, taken a loop that is repeated for N number of times where N is the number of characters in the array.

Each time (with each iteration i.e. from 0th position to N-1th position) a character is taken from the array and appended to the String. Then we have printed the string and its length. The resulting string will be “himani” and its length 6.

Setting character by character into a String Buffer

Sample Logic:

We cannot change the content of an existing String object. But we can change the content of a StringBuffer or StringBuilder object.

In the above example, we have taken a StringBuffer object that already contains some content. With the existing content, the length of the StringBuffer object is 11.

Then we have taken each character from the character array and stored in the locations 0,1,2,etc… it means, the letter ‘g’ (from 0th location of the array) replaces ‘l’ (the 0th letter in StringBuffer object) and ‘a’ replaces ‘e’, ‘n’ replaces ‘n’, ‘d’ replaces ‘g’, ‘h’ replaces ‘t’ and ‘i’ replaces ‘h’.

By the end of the for loop the replacements will be complete and the StringBuffer object contains “gandhiytext”. Then we have taken a substring of the StringBuffer object.

We have taken the first 6 letters of the StringBuffer object and left the remaining letters. So, by the time we go for printing, we will have “gandhi” in the StringBuffer object and its length will be 6.

Using the static method copyValueOf() of String class

This is a simple static method in String class that converts a character array data into a String object.

If we want only part of the data from the character array to be copied (created) to the String, then we can use an overloaded version of copyValueOf() that takes the character array and two integers as arguments.

The first integer indicates the location from where to copy and second integer indicates the number of character to consider.

For Example:

In the above example, we have 3 arguments to valueOf(). The first one is character array. Out of the two integers, first one (2) is the starting location and the second one (5) is the number of characters to consider. With the above example, the new String will contain “wtham”. Similarly the following example creates a new string “wth”.(it means the 3 characters from 2nd location)

Using the static method valueOf() of String class

This is also similar to copyValueOf(). This method also has overloaded versions like valueOf(char[],int,int) and valueOf(Object). Both of them can serve the purpose.

techhipbettruvabetnorabahisbahis forumutaraftarium24eduseduseduseduedueduseduseduedusedu
x

Check Also

What is Recursion In Java Programming – JavaTutoring

What is Recursion In Java programming – Here we cover in-depth article to know more ...

betwoonhacklink satın albets10 girişakcebet uyeliksupertotobet1xbet girişmelbet girişbetwinnerpinup uyelikbtcbahis girişmostbet girişbetwinner girişxslotskyloft uyelikmegapari uyeliktipobetgrandpashabetradissonbetmariobett.mebetandreasenbetcasivalmisbahismyhitbetonbahistipobetbahsinevavepasgolgrandpashabetmarsbahispusulabetcasibomcasibom girişcasibom güncel girişcasibomcasibom girişdeneme bonusu veren sitelerhttps://mobilebarhawaii.com/ballettea tropazmegabahis güncel girişmatadorbetbets10betturkeycasibomkingroyalbets10 girişbets10bets10maldives casinohacklinkgrandpashabetbetmatik girişmelbetmostbetmegapari üyelikistanbul escortmatadorbetjojobetJojobetjojobetpinco girisonwinonwin girişcasibom güncel girişselcuksportshdcasibomcasibomatakoy escortatakoy escortsisli escortgalatabahisliste paris sportifjeux casino gratuit avec bonusSimsinos Casinojojobet girişCashimashi AvisWunderWinsparier sur les cornersGamdomCasibommadridbet girişinterbahis 1606 compinbahisemiratesbettrbetcasibomcratosroyalbetpalacebetcasinokaliprensbetmostbet guncel girisbetandyou guncel girisromabet guncel giris7slots guncel girisbizbet guncel girisrocksroyalbetkazandradeneme bonusu veren sitelergrandpashabetonwinprincessbethttp://nakanakadining.com/betkanyonpinup yeni girismatadorbet yeni girisroketbet yeni girisskyloft yeni girismegapari yeni girissavoybettingcasinomhubligobetmavibetenobahisJojobetpusulabetjetbetonwin girişkingroyal girişarnavutköy escortkingroyalmaltcasinoholiganbet girişsahabetjojobethttps://www.escortbayanlariz.net/beylikdüzü escortfikstürbetmisliwinmarsbahisbets10meritkingsekabet girişvbetsahabet girişholiganbetmeritkingjojobetjojobetholiganbetholiganbetbets10Jojobet Girişmariobetodeonbetodeonbetbetwoonjojobetsahabetkingroyalbets10 girişjojobetmeritking girişsekabetsahabetcasibomcasibom girişholiganbet girişjojobet girişholiganbet girişjojobet girişarnavutköy escortGebze escort jojobetjojobet girişjojobet girişbetebetotobetimajbetcasibombetsatbakırköy escortmatbet girişsavoy kingbettingsüperbetin güncel girişsekabetpusulabetMavibetOnwinbets10 girişcasibomextrabetcasibomdeneme bonusu veren sitelerholiganbet girişbetturkey girişbetturkeyhalkali escortmarsbahis girişasyabahiscasibom güncel girişsahabetmatadorbetjojobet1xbet giriştipobet girişmeritking girişmadridbet girişmadridbet girişfixbet girişbetturkey girişbetkanyon girişbetebet girişvaycasinokralbetholiganbetjojobetcasibom girişjojobet girişpadişahbetbets10 girişbets10onwinselçuksportsSekabetholiganbethttps://www.gvscolombia.com/güvenilir casino siteleri antalya escortmadridbetmatadorbetdumanbetganobetganobet girişiptviptvcashbackiptvankara escort bayaninterbahiscasibomgrandpashabetcasival girişmarsbahismatadorbetbetofficeJojobetmaldives betcasibom girişmeritkingbetciojojobet girişgrandpashabet girişbahiscasino girişmegabahis güncel girişbetorderBankobetBetordercasibom 811.comtaraftarium24holiganbetholiganbetholiganbetjojobetzbahisGrandpashabet güncel girişcasibomjojobetonwinextrabetmatbetextrabet girişmatbet girişbetwoonmarsbahisbets10TipobetpusulabetSekabetcasibomesenyurt anal escortrus escortzbahisGrandpashabetholiganbetjojobet girişcasibomgüvenilir casino siteleri grandpashabetİstanbul Escortsekabet girişhttps://www.gvscolombia.com/marsbahissahabetdeneme bonusu veren sitelerfixbet girişrestbetmarsbahis girişstarzbetjojobetjojobet girişmrbahismrbahisJojobetsahabetjojobetjojobet girişgooglecasibomkucukcekmece escortartemisbetbetciocasinomaxigrandpashabetimajbetsekabetmaltcasinomatadorbetmatbetmavibetmeritbetmobilbahisonwinpinbahisizmir escorttexas sweepstakesflorida sweepstakessweepstakes sportsbooksweepslotsbankobet girişbankobetbankobet girişPusulabet güncel girişnakitbahisbets10 güncel girişmadridbetmeritkingsekabet güncel girişfixbetholiganbetsahabetholiganbetsahabetmarsbahis güncel girişholiganbet güncel girişjojobet güncel girişmarsbahis güncel girişsekabet girişvaycasinotuzla escort bayancanlı bahiscasibomimajbetstarzbetmobil ödeme bozdurma