• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Right way to concat Strings and then substring them ?

 
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



This code is like a snapshot from problem domain, what i am doing is concating strings with | (pipe) character. Then separating them as per my need.

my question- is there any better way to accomplish this ? As there can be scenario's where user can enter | from UI.

is there any other way to do concat with some other logic, so that whatever user enters from UI, can't break my code.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One approach would be to create a class that contains two String members. That way, no string operations need to be done at all, an dthe user can't muck things up no matter what they enter.

Or you can use a String[] or List<String>.
 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Ulf, for your reply. For me concatenating is important. As further down the stream, i will be comparing this string to another string.

Sorry i didn't got your approach. Can you explain in little detail.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I mean is either

String[] strings = new String[] { "abc", "def" }

or

List<String> strings = new ArrayList<String>();
strings.add("abc");
strings.add("def");

Either of these approaches would lend it itself easily to whatever comparison you need to do. So I don't think that's a valid objection.
 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ulf, i got your point. but it will require me to change my existing code structure.
I will take care of it.
 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf,

Concatenating them through pipes is also not a bad approach. the reason i am asking this is, because i have done it this way.
whenever there are 2 different logic's in front of me, which is more better. it always haunt me, which approach to follow.

Do this happen with all Java Developers?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case it requires making at least one conscious decision: can the strings possibly contain the "|" character? If not now, maybe later? (For example, if those are user inputs, have they been sanitized not to contain it, even if they shouldn't contain it in the first place, like if they're names or numbers?)
If the answer is potentially "yes", then the concatenation approach breaks down, or requires changes later (if at that time anyone still remembers that that particular character was used for this purpose).

I'll admit that I have used similar approaches in the past, but nowadays I think that if the data is structured then generally the data container should be structured (and not several pieces of data get crammed together).
 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Ulf for your reply. In my case, special characters are not allowed to enter.

I actually end up in doing like this, using the multidimensional array-

String[][][] name = {{{"", "", ""}}};

ArrayList<String[][][]> aLSpecCO;

[0][0][0], 1st position will hold String 1, 2nd position will hold String 2 and 3rd position will hold concatenated strings.
 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ulf,

As per your Suggestion, i am avoiding the use of pipe.

my new program will work like this sample model-





Do you like this approach ?
 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please advice.














SCJP, SCWCD, SCBCD

 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Amandeep Singh wrote:please advice.



Why do you need a String[][][] for this? Why not a plain old String[] like Ulf suggested?
 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please see my program, why i am using it. to hold concatenated strings.

what i am asking is, is it good idea to use three dimensional array?
 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you think that it is a good idea to use 3-D array? As Garrett specified this can be done using simple 1-D array.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Amandeep Singh wrote:please see my program, why i am using it. to hold concatenated strings.

what i am asking is, is it good idea to use three dimensional array?



I see your program, and in the example you presented, there is no need for a three dimensional array. Whether it's a good idea or not, you're not gaining anything over a plain old 1 dimensional array.

If this data structure is to be long lived, you may as well encapsulate the behavior you want in a class. Then at least the method names would provide more self-documentation than the indexes of an array.

 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see this approach also fine as mentioned by you. but the thing is, i have to get the value's from the String[] everytime then concatenate them.

whereas in my approach, at one time only i am putting the concatenated string in array. so everytime i need it, i get it from there and use it.

in your case i agree it looks intutive.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The concatenation should be a pretty fast operation. But you still haven't replied about why you think you need a String[][][] for this.
 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i need it because it will be like this-



so this is pretty simple way of doing concatenation.
instead of iterating through the String array's.

Because i am already putting String array inside List.
So here if i make again String array, inisde List. then i have to iterate through them and also concatenate them
which make's it elaborated.

but in my case, 1 line of code solve the purpose.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Amandeep Singh wrote:i need it because it will be like this-



so this is pretty simple way of doing concatenation.
instead of iterating through the String array's.

Because i am already putting String array inside List.
So here if i make again String array, inisde List. then i have to iterate through them and also concatenate them
which make's it elaborated.

but in my case, 1 line of code solve the purpose.



But you're only using 1 dimension of the array. You could just as easily do this:

 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Garrett Rowe wrote:

Amandeep Singh wrote:i need it because it will be like this-



so this is pretty simple way of doing concatenation.
instead of iterating through the String array's.

Because i am already putting String array inside List.
So here if i make again String array, inisde List. then i have to iterate through them and also concatenate them
which make's it elaborated.

but in my case, 1 line of code solve the purpose.



But you're only using 1 dimension of the array. You could just as easily do this:



No, i am using 3 dimensional array, you are using 1 dimensional array.
Yah that's true, using 1 dimensional array as in your last post is more simpler than 3d array and solving the same purpose.
thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic