Lab 1

correct the errors in the program.

/* Names.java */

import java.io.*;

/** The Names class provides a single function, main, that will
 *   perform various manipulations of the name John Fitzgerald Kennedy. 
 *   This is a modification of the program on page 43 of Arnow and Weiss.
 */

class Names {

/** Performs various string operations on the name John Fitzgerald Kennedy.
 *  @param arg is not used.
 */
  public static void main(String arg[]) {
    String first = "John";
    String middle = "Fitzgerald";
    String last = "Kennedy";
    String initials;
    String firstInit, middleInit, lastInit;

    firstInit = first.substring(0,1);
    middleInit = middle.substring(0,1);
    lastInit = last.substring(0,1);
    initials = firstInit.concat(middleInit);
    initials = initials.concat(lastInit);

    System.out.println();
    System.out.println(first + " " + middle + " " + last + " ");
    System.out.println(initials);
    System.out.println(last + ", " + first + " " + middle);
    System.out.println(last + ", " + first + " " + middleInit +".");
    System.out.println(first.toUpperCase() + " " + last.toUpperCase());

    System.out.println(first + " equals john is " + first.equals("john"));
    System.out.println(first + " equals john (ignoring case) is " 
               + first.equalsIgnoreCase("john"));
    System.out.println("The character at index 3 in " + middle + " is " +
               middle.substring(3,4));
    System.out.println("The index of \"gerald\" within " + middle + " is " +
               middle.indexOf("gerald"));
    System.out.println("The index of \"gerald\" within " + last + " is " +
               last.indexOf("gerald"));

    System.out.println();
  }
}

这里写图片描述“`

class String的一些method:

(1) substring

public String substring(int beginIndex, int endIndex)

Returns a string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

Examples:
"hamburger".substring(4, 8)
returns “urge”
"smiles".substring(1, 5)
returns “mile”

(2) concat

public String concat(String str)

Concatenates the specified string to the end of this string.
If the length of the argument string is 0, then this String object is returned. Otherwise, a String object is returned that represents a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string.

Examples:
"cares".concat("s")
returns “caress”
"to".concat("get").concat("her")
returns “together”

(3) toUpperCase

public String toUpperCase()

Converts all of the characters in this String to upper case using the rules of the default locale.

Examples:
"love".toUpperCase()
returns “LOVE”

(4) equals

public boolean equals(Object anObject)

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

Examples:

String s = "John";
s.equals("john")

returns false

(5) equalsIgnoreCase

public boolean equalsIgnoreCase(String anotherString)

Compares this String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length and corresponding characters in the two strings are equal ignoring case.

Examples:

String s = "John";
s.equalsIgnoreCase("john")

returns true

(6) indexOf

public int indexOf(String str)

Returns the index within this string of the first occurrence of the specified substring. The returned index is the smallest value k for which:

Examples

String s = "hello world!";
s.indexOf("wo")

returns 6

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值