Solved

Newbie question : comparing a string and a character

Posted on 2002-04-19
5
222 Views
Last Modified: 2010-03-31
What's the quickest way to compare a string with a character for equality. I have a string and I want to see if it contains a newline, carriage return or space character.

Thanks
0
Comment
Question by:gng
  • 2
  • 2
5 Comments
 

Expert Comment

by:mrowka
ID: 6953468
try using the indexOf method of String:

if s.indexOf("\n") returns a -1, then it
doesn't contain a newline,

the same should work with carriage return and space:
s.indexOf("\r")
s.indexOf(" ")
0
 

Accepted Solution

by:
mrowka earned 50 total points
ID: 6953474
0
 
LVL 9

Expert Comment

by:Venci75
ID: 6953477
String data = "your string";
boolean contains_cr = (data.indexOf('\r') >= 0);
boolean contains_new_line = (data.indexOf("\r\n") >= 0);
boolean contains_space = (data.indexOf(' ') >= 0);
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7972420
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by: mrowka
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venci75
EE Cleanup Volunteer
0
 

Expert Comment

by:SpideyMod
ID: 8021353
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…

762 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

21 Experts available now in Live!

Get 1:1 Help Now