Solved

Newbie question : comparing a string and a character

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 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…

785 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