Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Java by efinn ( 5 years ago )
/**
 * 
 * This class shows you how to find the location of a character in a string.
 * @author efinn
 * @date 3/12/21
 */
public class IndexString
{
    public static void main(String [] args)
    { 
        String word = "watermelon";
        
        //suppose I wanted to know the location (aka index) of where "r" is in word
        
        int location = word.indexOf("r");
        
        System.out.println("r had index " + location + " in " + word);
        
        //Suppose you wanted the last 4 digits in a phone number formated like this
        String phoneNum = "(781) 636-1248";
        
        int dashLoc = phoneNum.indexOf("-"); 
        
        System.out.println("The last four digits of PhoneNum are " + phoneNum.substring(dashLoc + 1));


    }
}

 

Revise this Paste

Your Name: Code Language: