• 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

substring()

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class x {
public static void main(String args[]) {
String s = "hypertext";
String t = s.substring(2,5);
System.out.println(t);
}
}
the result of this code should be "perte".But it displays "per". Please explain the rule for substring(int startindex,int endindex) method.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should download the Java API.
Quoted:

Hungson Le
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that the substring functions arguments substring(x, y) are x = the start point of where you would like to retrieve data (starting at "p"). Y = the data to return from the beginning of the complete String (starting at "h" but only returning "per" based on argument x).
 
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class x {
public static void main(String args[]) {
String s = "hypertext";
String t = s.substring(2,5);
System.out.println(t);
}
}
this is how it works , substring takes 2 args , first one the starting position 2nd one is the ending postion. What happends is when you say 2 it doesn't include 2nd position it starts including from 3rd so you get a p but it does include the last position so that's your result
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Shabbir.
When it comes to substring's two-argument version, I remember two things:

  • Substring, like arrays, starts counting from zero, so if the first argument is "2", substring starts counting at the third character;
  • The number of characters in the resultant String will be endIndex minus startIndex.

  • Hope this helps,
    Art
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! Shabbir
Here is the API documentation from the sun API pages:
Hope this helps
"substring
public String substring(int beginIndex,
int endIndex)
Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.
Examples:
"hamburger".substring(4, 8) returns "urge"
"smiles".substring(1, 5) returns "mile" "
 
Something about .... going for a swim. With this tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic