• Post Reply Bookmark Topic Watch Topic
  • New Topic

split a string using delimiter..truncate each segment and append  RSS feed

 
Preetish Panda
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.. this is my first post here ..I'll explain what I want to accomplish using an example

Suppose I have a string C:\check out\checking out\BACKUPER

Now I'll split it using delimiter as \ .
So it will become

  • C:

    check out

    checking out

    BACKUPER


  • "check out" and "checking out" contain space.The code will remove the space and convert
    them into "checkout" and "checkingout".

    Then truncate them into strings of 6 characters and append ~1.

    Finally they will be become checko~1 and checki~1.

    BACKUPER doesn't contain space.So, it will remain as it is.

    Finally all will be appended and result will be C:\checko~1\checki~1\BACKUPER


    Can any one suggest me an approach or any function that will make this adventure like a cake walk ??

    Thanks in advance ..
     
    Christophe Verré
    Sheriff
    Posts: 14691
    16
    Eclipse IDE Ubuntu VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Welcome to the ranch !
    If you look at the java.lang.String API, you'll have most (all?) of the tools you need.
     
    David Newton
    Author
    Rancher
    Posts: 12617
    IntelliJ IDE Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Seems like you have all the steps--what, specifically, are you having a problem with?

    Be careful with this algorithm, though--what would happen if you had these two directories?

    c:\check out\foo
    c:\check out this\foo
     
    Preetish Panda
    Greenhorn
    Posts: 9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    David Newton wrote:Seems like you have all the steps--what, specifically, are you having a problem with?

    Be careful with this algorithm, though--what would happen if you had these two directories?

    c:\check out\foo
    c:\check out this\foo


    For the directories given by you ... according my algorithm it will be
    c:\checko~1\foo
    c:\checko~1\foo

    As foo is devoid of space.

    I was just looking for some tip or approach that will be quite easy.
     
    David Newton
    Author
    Rancher
    Posts: 12617
    IntelliJ IDE Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Right, what I'm saying is that the two different directories would map to the same shortened string, which is a Bad Thing.
     
    Preetish Panda
    Greenhorn
    Posts: 9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    David Newton wrote:Right, what I'm saying is that the two different directories would map to the same shortened string, which is a Bad Thing.


    Ok.. In my case this kind of scenario won't arise.
     
    Raza Mohd
    Ranch Hand
    Posts: 247
    Java MyEclipse IDE MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator



    hi ,

    I hope this could help.
    I tried to get the desired output.

    regards
    raza!
     
    David Newton
    Author
    Rancher
    Posts: 12617
    IntelliJ IDE Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Raza: Please don't provide complete solutions. Our goal on JavaRanch is to help people learn how to learn, figure out problems, and so on--rather than just do somebody's work for them, encourage them to spend some quality time on it themselves, try various solutions, etc. Thanks.
     
    Raza Mohd
    Ranch Hand
    Posts: 247
    Java MyEclipse IDE MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    okay Newton,

    i will not do it again,
    it was my mistake.

    thanks
    raza!
     
    Ritesh Pareek
    Ranch Hand
    Posts: 50
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    For the directories given by you ... according my algorithm it will be
    c:\checko~1\foo
    c:\checko~1\foo


    No, This must be like


    c:\checko~1\foo
    c:\checko~2\foo

    java.lang.String API having this solution. Algo should be look like this

    loop1 while mainStr not null
    resArr[count++] = mainStr.slice(indexof("\\"))
    end Loop2


    loop2 while resArr

    1. you can use selection/binary sorting here
    2. truncate tailing chars with ~1
    3. do compare array element for 8 char similar string.
    4. if similar truncate tailing chars with ~2,3,4... so on
     
    Campbell Ritchie
    Marshal
    Posts: 56518
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Apology accepted, on behalf of JavaRanch
     
    Preetish Panda
    Greenhorn
    Posts: 9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ritesh Pareek wrote:
    For the directories given by you ... according my algorithm it will be
    c:\checko~1\foo
    c:\checko~1\foo


    No, This must be like


    c:\checko~1\foo
    c:\checko~2\foo

    java.lang.String API having this solution. Algo should be look like this

    loop1 while mainStr not null
    resArr[count++] = mainStr.slice(indexof("\\"))
    end Loop2


    loop2 while resArr

    1. you can use selection/binary sorting here
    2. truncate tailing chars with ~1
    3. do compare array element for 8 char similar string.
    4. if similar truncate tailing chars with ~2,3,4... so on


    You are not getting me .In my case there will be only one string and only ~1 has to be appended.
    No question of ~2,~3,...

    Thanks for your suggestion.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!