String length help

Hi I am trying to compare two strings to see if they both end with "\r\n" one is being read from the command line the other is a fixed string, but I am having the following problem

publicstaticvoid main(String[] args)

{

Scanner s =new Scanner(System.in);

String a ="\r\n";

String b = s.nextLine();// input \r\n

System.out.println(a.length());// 2

System.out.println(b.length());// 4

System.out.println(a.equals(b));// false

}

Idealy the two should be the same length. Any ideas on how to do this.

[901 byte] By [finlowbaa] at [2007-11-26 21:55:41]
# 1

> String a = "\r\n";

That ISNT the text \r\n.

\ = escape character

\r = special code for carriage return

\n = special code for carriage return

The escape tells Java to use those special values.

To escape the escape add another \

As in \\r

> String b = s.nextLine(); // input \r\n

This IS the text \r\n

When you type \r\n the \ arent escaped.

to test if a String ends with \r\n use:

boolean endswith = yourString.endsWith("\r\n");

TuringPesta at 2007-7-10 3:51:42 > top of Java-index,Java Essentials,New To Java...
# 2

import java.util.*;

public class Escape{

public static void main(String[] args){

String str1 = "\r\n";

String str2 = "\\r\\n";

Scanner s = new Scanner(System.in);

System.out.print("Enter Text: ");

String str3 = s.nextLine();

System.out.println("\nString 1: '" + str1 + "'");

System.out.println("String 2: '" + str2 + "'");

System.out.println("String 3: '" + str3 + "'");

System.out.println("\nLength 1: " + str1.length());

System.out.println("Length 2: " + str2.length());

System.out.println("Length 3: " + str3.length());

System.out.println("\nEnds With: " + str1.endsWith("\r\n"));

System.out.println("Ends With: " + str2.endsWith("\r\n"));

System.out.println("Ends With: " + str3.endsWith("\r\n"));

System.out.println("\nEnds With: " + str1.endsWith("\\r\\n"));

System.out.println("Ends With: " + str2.endsWith("\\r\\n"));

System.out.println("Ends With: " + str3.endsWith("\\r\\n"));

}

}

TuringPesta at 2007-7-10 3:51:42 > top of Java-index,Java Essentials,New To Java...
# 3
Thanks for the help all is clear now
finlowbaa at 2007-7-10 3:51:43 > top of Java-index,Java Essentials,New To Java...