简体   繁体   中英

How do I compare the output with a string in java?

I have written a program that gives an output like

"bbccaa"

Is there a way in java to store this into a sting and compare to another string.

What do you mean by 'gives an output' ? Does it print the string to the console?

Assuming that you have something like:

String s = ...; // Something that creates bbccaa
System.out.println(s);

Then you can just do:

s.equals(otherString);

to compare the two strings

Use equalsIgnoreCase() or equals() to compare 2 String objects ....

String s = "hello";

String sOutput = "bbccaa";

if (sOutput.equalsIgnoreCase(s)){   // Ignore case while comparing


 }


if (sOutput.equals(s)){            // Consider case while comparing


 }

How about

"bbccaa".equals(anotherString)

or

"bbccaa".compareTo(anotherString)

See

How is this string generated? If you use Streams you save the stream as a string and use the equals-Method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM