Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Newbie question : comparing a string and a character

Posted on 2002-04-19
5
Medium Priority
?
266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

671 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