aspose file tools*
The moose likes Beginning Java and the fly likes How to find index of a letter in a string? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to find index of a letter in a string?" Watch "How to find index of a letter in a string?" New topic
Author

How to find index of a letter in a string?

erin lang
Greenhorn

Joined: Sep 22, 2005
Posts: 7
Hi this is a newbie question. I have done many searches but none quite fit what I am looking for.

I have a string of some length that I do not know and will change during searches. I want to search a string and have application tell me what letter if any is in postion 4.

For example :

String names = "Hello how are you doing today?";

Of the string names I want to know what letter if any is in position 4? Which would be the letter o.

It was a bit confusing because I came away with too many different sugestions. Such as I have to use a hash table or use some complicted interation to go thruough the string. It seems to me that this would be one of the basic functions of Strings and should not be that complicated just to tell me what letter is at what index.

The second question In some of my searches suggestions were made to turning the string into an Array, but I thought a String is allready a kind of an array?

Any help much appreciated. What is the simplest way to go about doing this.

Thanks,
Erin
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
System.out.println("Hello World".charAt(4));
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

...I have done many searches but none quite fit what I am looking for.

Did you check the API documentation for String?

...I want to search a string and have application tell me what letter if any is in postion 4... It seems to me that this would be one of the basic functions of Strings...

It is. See charAt(int i).


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5



Always good to handle null/length exceptions directly before they get thrown.

By the way, by position 4 do you mean the 4th character or the string position of 4? Because charAt(4) actually returns the 5th character due to 0 offset.
[ January 04, 2006: Message edited by: Scott Selikoff ]

My Blog: Down Home Country Coding with Scott Selikoff
erin lang
Greenhorn

Joined: Sep 22, 2005
Posts: 7
Thank you for responce..

As for the checking doc. I am aware of there existance and check them often, unfortunatyl they are not always clear. I find them to be confusing sometimes lacking in examples. Some of us just need stuff explained a different way or by someone else. Many of the doc just dont seemed written for a beginner in mind. Of course maybe they were justnot written for beginners in mind, in that case it is good to ask another.

Another question.

Your suggestion works for telling me what is in certain position and i tried using substring which alos works, but how would i test if it is a blank space?

For example:

String names = "Hello how are you doing today?"

names.charAt(5);

I get back nothing. I assume this is because in postion 5 there is a blank space. How can i test if position 5 is a blank space. I did the.

if (e.charAt(5) == " ") {
System.out.println("blank space");
}else {
System.out.println(e.charAt(5));
}

Prints out nothing. Why? How do I get it to recognize there a blank space?

Thank for help.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by erin lang:
... As for the checking doc. I am aware of there existance and check them often, unfortunatyl they are not always clear...

Many users find the API documentation difficult at first, but I think you will be surprised at how quickly you get comfortable with it if you really dig in and learn to use it.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by erin lang:
...if (e.charAt(5) == " ") {
System.out.println("blank space");
}else {
System.out.println(e.charAt(5));
}

Prints out nothing. Why? How do I get it to recognize there a blank space?

The charAt method returns a char. But a literal (in this case, a space) enclosed in double quotes is treated as a String. A char literal should be enclosed in single quotes.

if(e.charAt(5) == ' ')...

Note: Actually, your example above is not printing "nothing." It's printing charAt(5), which is a space.
[ January 04, 2006: Message edited by: marc weber ]
erin lang
Greenhorn

Joined: Sep 22, 2005
Posts: 7
thanks again for reply

this also works

public class TestString {
public static void main(String[] args) {
String names = "Hello how are you doing? ";
if (!Character.isWhitespace(names.charAt(5))) {
System.out.println(names.charAt(5));
}else {
System.out.println("white space");
}
}
}

I found isWhitespace in docs but again as an example not very helpful.
You assume I am new to java, I am not I just asked a newbie question.

I have not had straight forward learning in java I have had to hop around to get things done. I do use the docs and they have helped occasionally but like many docs its just not written in the way I think. I think differently then most therefore they are not the holy grail for explaination for me. Yes I did look at docs first, but could not find what I needed so I came here were it is ok to ask questions even dumb ones. This site has helped much in past.

Thanks again.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by erin lang:
...I came here were it is ok to ask questions even dumb ones. This site has helped much in past...

There are no dumb questions here -- ask whatever you like! I'm glad you find the site helpful.
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

Originally posted by marc weber:

There are no dumb questions here -- ask whatever you like! I'm glad you find the site helpful.


Really? How about poorly-formed questions, do those count?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Scott Selikoff:
... How about poorly-formed questions, do those count?

Not when we're all being nice.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to find index of a letter in a string?