Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Some help with Strings would be nice!

  Asked By: Gene    Date: Oct 04    Category: Java    Views: 1014
  

I have a few simple (!) problems I hope that you can
help me with:

(1) Finding a substring (say, aString ='efg') within the main string
(eg, mainString = 'abcdefghijklm'). The mainString will have NOT have
a fixed range of characters.

I've tried using the index of, but I think I'm doing it wrong. What I
want is to be able to see if the subString matches ANY portion of the
mainString.

(2) I know how to get length of a string, but is there a way to
answer in an integer format (which I'd use in a FOR statement later
on)? Eg. I get a length of 5 - how can I convert that into a integer
variable. Note: I don't want to alter the String! Just get a integer
output for length.

(3) Convert a String into separate characters. Eg a string
of 'abcde'. would be converted into five DISTINCT characters A B C D
E that I could then perform calculations against. Again it's not
fixed.

Any help of this would be most welcome.

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Topaz Ramirez     Answered On: Oct 04

> (1) Finding a substring (say, aString ='efg') within the main string
> (eg, mainString = 'abcdefghijklm'). The mainString will have NOT have
> a fixed range of characters.

Do a Google search on "string matching" or "pattern matching". There
are many algorithms for doing this. Some simpler than others.

> (2) I know how to get length of a string, but is there a way to
> answer in an integer format (which I'd use in a FOR statement later
> on)? Eg. I get a length of 5 - how can I convert that into a integer
> variable. Note: I don't want to alter the String! Just get a integer
> output for length.

String.length() returns an int.

>
> (3) Convert a String into separate characters. Eg a string
> of 'abcde'. would be converted into five DISTINCT characters A B C D
> E that I could then perform calculations against. Again it's not
> fixed.

Here's the code to turn a String into a char array. char may be
replaced with the Character wrapper class.

char cArr[] = new char[s.length];
for(int i = 0; i < s.length(); i++){
cArr[i] = s.charAt[i];
}

 
Answer #2    Answered By: Angie Bennett     Answered On: Oct 04

> (1) Finding a substring (say, aString ='efg') within the main string
> (eg, mainString = 'abcdefghijklm'). The mainString will have NOT have
> a fixed range of characters.
>
> I've tried using the index of, but I think I'm doing it wrong. What I
> want is to be able to see if the subString matches ANY portion of the
> mainString.

int i = mainString.indexOf(aString);
if (i != -1) {
// Found an instance of aString in mainString at 'i'.
}

If you need to find multiple instances of aString in mainString then
you can do the same thing but in a loop until you reach the end of the
string.

>
> (2) I know how to get length of a string, but is there a way to
> answer in an integer format (which I'd use in a FOR statement later
> on)? Eg. I get a length of 5 - how can I convert that into a integer
> variable. Note: I don't want to alter the String! Just get a integer
> output for length.

int length = str.length();

length will contain an integer value the represents the length of the
string 'str'.

BTW, To convert a string representation of an integer:

String str = "14";
int i = Integer.parseInt(str);

'i' will now contain the value 14.

>
> (3) Convert a String into separate characters. Eg a string
> of 'abcde'. would be converted into five DISTINCT characters A B C D
> E that I could then perform calculations against. Again it's not
> fixed.
>

char c[] = new char[str.length];
str.getChar(0, str.length, c, 0);

> Any help  of this would be most welcome.

I would suggest that you get the JDK API docs.

 
Answer #3    Answered By: Ray Lawrence     Answered On: Oct 04

Thanks to everyone for their help. The help  provided is, as always,
most useful.

 
Didn't find what you were looking for? Find more on Some help with Strings would be nice! Or get search suggestion and latest updates.




Tagged: