• Post Reply Bookmark Topic Watch Topic
  • New Topic

String Manipulation  RSS feed

 
Jim Hooper
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok guys, this seems a fairly basic question (only working without a book and online ref) but I can't find the answer!

I want to know what a section of a string is equal to,:

eg

if string TEST is given I want to be able to get the 3rd char. what function would I use?

Many thanks for help, tips, guidance and sleepless nights!
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
charAt(2);

probably want to put a check to make sure the String your getting is at least that long.
 
Jim Hooper
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

Assume then that the 1st 'T' is considered 0 as in array declarations?
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Strings have base 0 indexing. Also, String.length() will return the number of chars so if you try a charAt(.length()) you will get an IndexOutOfBoundsException.

so if you are looking at all the chars in a String it would be something like:

String s = "string";
for(int i = 0; i < s.length(); i++){
//char code here
}

Another nice thing about Strings is that a literal String, something in quotes, is also a String object that you can call methods on.

if("MyString".equals(s)){
}

doing this like this for comparison can help eliminate NullPointerExceptions.
 
Jim Hooper
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mmmm...Steven, think I'm missing something here...my code is as follows

public static void main(String[] args)
{
Stringnumword = args[0];
Stringoutput;
int i;
int i1;
int i2;
for (i = 0; i < numword.length; i++)
{
if (numword.charAt(0) = "1")
{.......

but will not accept numword.charAt(0). Error -
Java_4a.java:31: cannot resolve symbol
symbol : method At (int)
location: class java.lang.String
if (numword.At(0) = "1")
^

Any help greatly appreciated!
Thanks again....
 
John Dell'Oso
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,

Some things to fix in your code - the charAt() method returns a character and in your if statement, you are comparing it to a string (the double quotes mean String). Secondly, you are not doing a comparison in your if statement - you are specifying an assignment. You need to use ==. Also, you need to add parantheses to the length method call - numword.length().

Here's the "fixed" code:



Hope this helps.
JD
[ January 05, 2005: Message edited by: John Dell'Oso ]
 
Jim Hooper
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou JD and everyone else. However, I've now moved on!

Taking the TEST example again, charAt(n) only gives me one charachter. What do I need if I want say the 2nd and 3rd chars? ie return ES

Is it something like substr(n, n) ?

Many thanks
Jim
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your on the right track. Look over the javadocs.

Here is for String:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

Here is for all:

http://java.sun.com/j2se/1.4.2/docs/api/
 
Jim Hooper
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou Steven, again, I will now scuttle off and apply to code
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!