Solved

Newbie question : comparing a string and a character

Posted on 2002-04-19
5
264 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 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
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

630 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