-1

I've an array with hundreds of string values, Is it possible to use specific formats to make them shorter?

e.g

Normal -> "Hello John, What's up?!"

With short format -> "Hello John..."

After using substring, I got errors.

private String[] finalString;
for (int i = 0; i < arrays.PodcastTitle.length; i++) {
    finalString[i] = arrays.PodcastTitle[i].substring(0, 5); 
}
iSun
  • 1,714
  • 6
  • 28
  • 57

3 Answers3

2

Since you haven't given any details, this is the shortest approach :

String little = normalString.substring(0, 10); // use anything 5 or 10 or 15, depending on how short you want to make your String

From your edit:

Please change:

private String[] finalString;

to:

private String[] finalString = new String[whateverSizeYouWant];
Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
2
String toLitteString(String str, int length) {
     if (str.length() > length) return str.substring(0, length) + "...";
     return str;
}

Function that will truncate longer strings to length (and add a "...") or return the short string. If you want the length to include the "..." then just change length to length - 3.

Raekye
  • 5,081
  • 8
  • 49
  • 74
0

Why won't you consider implementing a method that takes a String argument, explodes it by space character and returns the String with required number of words in it?

iozee
  • 1,339
  • 1
  • 12
  • 24