Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Gene Taylor   on Oct 04 In Java Category.

  
Question Answered By: Angie Bennett   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.

Share: 

 

This Question has 2 more answer(s). View Complete Question Thread

 
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: