Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Newbie question : comparing a string and a character

Posted on 2002-04-19
5
Medium Priority
?
271 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 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

972 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