LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-02-2007, 08:37 PM   #1
thtr2k
Member
 
Registered: Mar 2005
Location: Australia
Posts: 71

Rep: Reputation: 15
Question plz help with JAVA String


i want to truncate some strings in JAVA to a certain length. How do i make use of JAVA API for this problem?
Code:
eg:
  if string length is more than 4 characters i just want to get the last 4.
   input: abcdaasd
   
   output: aasd
thank you.

Last edited by thtr2k; 11-02-2007 at 09:40 PM.
 
Old 11-02-2007, 08:56 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 237Reputation: 237Reputation: 237
substring perhaps? plus a combination of charAt.
see here for example
 
Old 11-03-2007, 12:15 AM   #3
devn
Member
 
Registered: Mar 2007
Location: Bangladesh
Distribution: Suse, Solaris
Posts: 40

Rep: Reputation: 15
Hi,
I guess this one should help:

Code:
String test = new String("teststring");
String test2 = new String("test");
String test3 = new String("timemachine32");
String test4 = new String("abc");

   if(test.length()>4)
   {
       System.out.println("test output: " + test.substring(test.length()-4));
   }
Compare the string lengths to get their individual outputs. I did for
variable test. Try for test2 and any other of your choice.

test4 and test2 should not yield anything when tested in if.
 
Old 11-03-2007, 01:04 AM   #4
thtr2k
Member
 
Registered: Mar 2005
Location: Australia
Posts: 71

Original Poster
Rep: Reputation: 15
my code is like this
Code:
...
String aString = "myTestingString";
String newString ="";
...
if(aString.length()>4){
       newString = aString.substring((aString.length() - 4), aString.length());
...
} else {
...
}

System.out.println("new string: " + newString);
...
it works thanks guys.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to encrypt a string in Java me4linux Programming 1 09-03-2007 03:30 PM
Getting a class From a String in Java MRMadhav Programming 5 02-18-2006 05:50 AM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM
java + string tokenizer dave bean Programming 2 12-10-2003 03:22 PM
java covert double into string iceman47 Programming 10 05-28-2003 10:51 PM


All times are GMT -5. The time now is 06:47 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration