• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Popping top items from a string

GIR

Member
Hello,

I have a string which has data appended to it, and it displayed in a textview.

The problem is I need to display only the last 10 items appended to prevent fresh data being appended out-of-view.

The data is appended with eol, so I can spilt the string at that point.

I know I can use :
Code:
String aList[] = aString.split(eol);
which nicely places the string into the substring, so my question is:

How can I append only the last 10 items from the substring back into aString?

I've been all over google search trying to find a solution, but all my attempts have either given errorr in Eclipse, or thrown a force-close.

One way I tried was with:
Code:
aStringLength = aString.length;
int x = 0;
         x=x+20
        while(x != aStringLength){                    
            aTemp = aTemp +(aList[x]);
            x=x+1;
          }
Which is an iteration algorithm I frequently use in python, but is not successful in Java.

Many thanks for any suggestions.
GIR
 
so, if I am reading this right, you're splitting the string to remove the eol's ?

If so, why not then rejoin it with something like:

Code:
android.text.TextUtils.join ( delimiter, tokens );
(or some other method)

then do a substr(start,len); on the joined string... use len-10 for the start (or 0, whichever is greater)


=or=

use a string builder

Code:
int arraylen = myStringArray.length;
String builder sb= new StringBuilder();
for (int i = 0; i < arraylen ; i++ )
{
    sb.append( myStingArray[i] );
}

String myFullString = sb.toString();
int strlength = myFullString.length();
int start = strlength  >= 10 ? strlength-10  : 0; 

String myShortString = myFullString.substr( start,strlength );
I dunno I'm pretty tired at the moment I hope that code makes sense heh. I'll check it tomorrow too but gotta run.

hth
 
  • Like
Reactions: GIR
Close, but no.

Assume aString contains:
Code:
data1 + eol
data2 + oel
data3 + oel
data4 + oel
data5 + oel
data6 + oel
data7 + eol
data8 + eol
data9 + eol
data10 + eol
The goal is to get a list thus:
Code:
data5 +eol
data6 + eol
data7 + eol
data8 + eol
data9 + eol
data10+eol

and once I have that to get it back into a string...
 
OK, I guess I'm a bit confused as to what the goal is :)

Do you want a new array of strings as only the last 10 items from the original array?

Have you looked at System.arraycopy() ?
 
  • Like
Reactions: GIR
OK, I guess I'm a bit confused as to what the goal is :)

Do you want a new array of strings as only the last 10 items from the original array?

Have you looked at System.arraycopy() ?

Thanks for the tip.

I hadn't known about arraycopy, so I'll be having a stab at it today :)
All the best,
GIR
 
Back
Top