Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I get the left x characters from a string?  RSS feed

 
Lance Jurgensen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a built in method to get the leftmost x characters from a string? Like Left(myString, 25) ?
-Lance
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
something like this.....

String theString = "sdlj sadkjsakld jsajdksajdsjkdjsajdkjsadlk j";
String str1 = theString.substring(25,29);
 
Lance Jurgensen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah thank you. Being a newbie to Java/JSP I am still learning where to look. I ended up finding the subString() in the 1.4.2 api. I was looking in the 1.3.1 api.

The code I ended up with to strip off the first 75 characters and then append a suffix if the original length was larger. I also checked for the length to make sure it was long enough so I didn't throw an exception.

int iLgth = 75 ;
String sSuffix = "...";
String sSubject = rs.getString("chief_complaint");

if (sSubject.length() < iLgth) {
iLgth = sSubject.length() ;
sSuffix = "";
}
<td><%=rs.getString("chief_complaint").substring(0, iLgth)%><%=sSuffix%></td>

Now, I should probably make this into a method that takes start, length, string and suffix!

left(0,75,sSubject,sSuffix)

Thanks again,
-Lance
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The REXX language has left(input,length) that returns input truncated or padded to length. An optional third parameter gives a pad character. Here translating to Java ...

I've duplicated these functions (and a few more) in every new language I use since 1980. If you're going to do much formatted text output - say columnar reports - they are real handy.
[ July 22, 2004: Message edited by: Stan James ]
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lance Jurgensen:
Ah thank you. Being a newbie to Java/JSP I am still learning where to look. I ended up finding the subString() in the 1.4.2 api. I was looking in the 1.3.1 api.

-Lance


Err, here is the link to 1.3

substring() doc.
[ July 29, 2004: Message edited by: Sadanand Murthy ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arg. UBB won't render links with a parenthesis in it. So, Sadanand's post got a little mangled. (You can still just copy and paste the URI.)

To be clear, the point Sadanand is making is that the String.substring(int, int) method is certainly in the 1.3 J2SE API.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!