Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 585
  • Last Modified:

How to count the string characters in a JSP ?

Hello,

I have the following code in my JSP -

  if (total_path.startsWith("ost_vexdev_selects")) {
        s_partial_path = total_path.substring(total_path.indexOf("osts_season/System/AppilcationData/")+ 44 );
    } else {
        s_partial_path = total_path.substring(total_path.indexOf("osts_season/System/AppilcationData/")+ 36 );
    }


I would like to know how to change this code so instead of codign - "ost_vexdev_selects"  just check for "_selects " in a string

And instead of coding - 44 or 36  - I get that count from some java api function  like indeof or someting ... ?

Please advise..

Thanks!
0
sdesar
Asked:
sdesar
  • 6
  • 5
1 Solution
 
tomgalloCommented:
-To count the characters of a string use: lenght()
ie: total_path.lenght();

-For the if sentence use:
if(total_path.lastIndexOf("_selects")!=-1)

Hope that helps
-tom
0
 
sdesarAuthor Commented:
I need to get the length of this string = osts_season/System/AppilcationData/

This is the error I got ..

int cannot be dereferenced
          s_partial_path = total_path.substring(total_path.indexOf("osts_season/System/AppilcationData/").length() );


any suggestions ...
0
 
tomgalloCommented:
The error lies here:

indexOf() always returns a int... not a string, you can't do indexOf().lenght(), beacuse you only can take the lenght of a string (not an integer).

to get the length of that string do:
String dummy = new String("osts_season/System/AppilcationData/");
dummy.lenght();

The second part: I don't understand what you want to do but I think is almost like this:
s_partial_path = total_path.substring(total_path.indexOf(dummy)+dummy.lenght());

Clarify this for me!
-tom

 
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sdesarAuthor Commented:
Oh Thanks .. I got it  
s_partial_path = total_path.substring(total_path.indexOf(dummy)+dummy.lenght());


Also one more question
I have a string osts_season_selects.
I just want it to return "osts_season" without the _selects

any suggestions...
0
 
sdesarAuthor Commented:
One last question .

This is the string
"osts_season_selects"
I just want it to return
"osts_season" without the "_selects"

Please advise .. eagerly awaiting suggestions..
0
 
sdesarAuthor Commented:
I figured out the answer to the above question.

One last question ........
ost_visdev_selects/System/Application Data/Elements/osts_season/spot/characters/


I need to get "osts_season" from the above string... which should always be after "Elements/ " and end at "/" please advise

eagerly awaitng an answer

Thanks!
0
 
tomgalloCommented:
You should use aString.substring(int beginIndex, int endIndex)
To retreive the beginIndex you can use lastIndexOf(aString),
for the endIndex use indexOf("/",lastIndexOf(aString))

For more reference all the String class is in:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

Also you can use String dummy[] = aString.split("/") and search the first index after "Elements"'

-tom
0
 
sdesarAuthor Commented:
Thanks for the above I implemented it and I print the dummy[3] - it returns osts_season

I think this is a last question ..
This is the string
ost_visdev_selects/System/Application Data/Elements/osts_season/spot/characters/

and I want it to return everyting after - Application Data

Please advise.
0
 
tomgalloCommented:
If you do a split('Application Data') the [0] index is all the string before the first ocurrence of "Application Data"
0
 
tomgalloCommented:
Sorry I misread your question...

This is the answer: I suppose that your string is in aString variable,

aString.substring(aString.indexOf("Application Data")+16,aString.length());

All the answers to your questions are in the link that I posted earlier:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

Hope this helps,
-tom
0
 
sdesarAuthor Commented:
Thanks for the answers.
HEre are the excellent points
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now