Visit the LQ Articles and Editorials section
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 02-10-2004, 03:07 AM   #1
Registered: Feb 2003
Location: Atlanta, GA
Distribution: Debian 3.1
Posts: 149

Rep: Reputation: 15
Java Strings

Let's hypothetically say you have a simple string, ie "The.Dog.Ran."
and you ONLY want to get rid of the LAST period without altering the others. How would you do that easily? Preferably without the use of arrays. So basically the end result would be to display "The.Dog.Ran"
Old 02-10-2004, 05:40 AM   #2
Mega Man X
LQ Guru
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 64
Sure thing mate. You've to use .length() which returns the size of a string and .substring(start, stop) to read a string from the beginning till the end. Here is a working example:

public class Testing{
	public static void main (String[] arguments){
		String ie = "The.Dog.Ran.";

		System.out.println(ie.substring(0, ie.length()-1));
Old 02-10-2004, 05:44 AM   #3
Registered: Feb 2003
Location: Atlanta, GA
Distribution: Debian 3.1
Posts: 149

Original Poster
Rep: Reputation: 15
Old 02-15-2004, 01:08 PM   #4
Registered: Jul 2003
Location: Toronto, Canada
Distribution: Debian etch, Gentoo
Posts: 312

Rep: Reputation: 30
String s = ""
int idx = s.lastIndexOf(".");
String result = s.substring(0,idx); // this is now ""




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

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 05:04 AM
strings in c djgerbavore Programming 8 01-11-2005 05:27 PM
Java printing - problem with large strings Andy@DP Programming 2 08-03-2004 03:23 PM
Strings and int? kalleanka Programming 12 02-10-2004 05:34 AM
align columns of strings and explain '\t' (java) dave bean Programming 3 01-10-2004 11:55 PM

All times are GMT -5. The time now is 12:46 PM.

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