Solved

Is there a equivalent of strncpy() in Java?

Posted on 2000-04-24
4
2,559 Views
Last Modified: 2008-03-06
I have to remove carraige return from the end of the String, is there a method to do that?

Thanks.
Theta.
0
Comment
Question by:theta
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 20 total points
ID: 2745393
Assuming, as your question implies, that you know the string ends with a carriage return, the simplest solution would be:

String ret; //string with \n in it
....
//ret gets a value somehow
....

String noret=ret.substring(0,ret.length()-1);

0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2745409
Perhaps you could use String.trim(). This returns a new String with all whitespace removed from both ends of the source String. Whitespace here is the same as defined by the Character.isSpace() method: horizontal tab, newline, form feed, carriage return, and, of course, space (' '). Remember that this returns a new String. Since String is an immutable object, you cannot change its contents.

    String s = <your string>
    s = s.trim();

If you want to just remove carriage return, you might consider something like:

    String s = <your string>
    int crIndex = s.indexOf("\r");
    s = s.substring(0, crIndex);

This will take a substring of the original String that contains everything from the beginning of the String up to but not including the index of the first carriage return character. Of course, everything after the carriage return is discarded as well.

If you really want to remove just carriage returns and leave everything else intact, you might consider this:

    String s = <your string>
    StringBuffer buf = new StringBuffer(s);
    for (int index = buf.length() - 1; index >= 0; --index) {
        if (buf.charAt(index) == '\r') {
            buf.deleteCharAt(index);
        }
    }
    s = buf.toString();

If you want to remove just the last occurrence of carriage return, break the loop after deleteCharAt().

There is also a rather inefficient way of doing it with StringTokenizer but I doubt that it would be necessary to go to that length.

Best regards,
Jim Cakalic
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2745435
I was typing my comment when imladris posted so I didn't see his answer. Keep in mind that the carriage return and newline pair are usually seen in that order. As a result the posted answer will remove the last character from the String which is most likely going to be newline, not carriage return.

Playing around a little more, the simplest solution with the least side effects is likely to be String.replace():

    String s = <your string>
    s = s.replace('\r', '');

This will return a new String with all carriage returns removed.

Jim
0
 

Author Comment

by:theta
ID: 2745754
Imladris solution worked fine.

Jim, thankyou for such detailed description on strings. I tried your solutions and they worked too.

Theta.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now