English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have an arraylist with 5 cells with type String:
0: AU
1: ~T
2: H~
3: O
4: ~R

I want to combine them to make one string:
"AU~TH~O~R"

As a bonus I'd like to completely remove the tildes (~).

Any help would be appreciated. Thanks

2007-03-04 05:19:00 · 4 answers · asked by Anonymous in Computers & Internet Programming & Design

4 answers

you can concatenate strings with the overloaded "+" operator, so if you iterate through the array(i'm assuming an array list is the same thing as an array) you can put all the strings together. for example:
string tempstring = "";
for(int i = 0; i < 5; i++)
tempstring += arraylist[i];

that will set tempstring equal to "AU~TH~O~R"

to remove the tilde's you can use something like this
int blah = tempstring.find("~");
string tempstring2;
tempstring2 = tempstring.substr(0,blah) + tempstring.substr(blah,tempstring.length);
until tempstring.find("~"); returns string::npos
when that happens "~" does not exist in the string

hope this helps

2007-03-04 05:25:45 · answer #1 · answered by metalluka 3 · 0 0

declare a string variable.
put a for loop that goes 5 times(the length of the array)
concatenate the array variable to the string variable u declared.
thats all.
Ex:(in java)// let the array name be arr[]

String str;
for(int i=0;i { str = str+arr[i]; }

I think this would do. If u do not want the tildes, use ascii values to check the characters...

2007-03-04 05:28:02 · answer #2 · answered by s_vijendran007 1 · 0 0

You know you can use "for" loop or do it manually.
We assume that arrName is the name of your arrayList.
string str="";;
for(int i=0;i {
str=str+arrName[i];
}

Also you can do it manually:
string str;
str=arrName[0]+arrName[1]+arrName[2]+arrName[3]+arrName[4];

2007-03-04 09:18:54 · answer #3 · answered by Moein 3 · 0 0

In javascript it would be e.g.
var my_array=new Array();
my_array[my_array.length] = 'AU';
my_array[my_array.length] = '~T';
my_array[my_array.length] = 'H~';
my_array[my_array.length] = 'O';
my_array[my_array.length] = '~R';
var string=''; for (var i=0; i string+=my_array[i].replace(/~/,''); }

2007-03-04 06:07:24 · answer #4 · answered by fjpoblam 7 · 0 1

fedest.com, questions and answers