簡體   English   中英

僅通過使用charAt(lastIndexOf,concatenate,substring,contain)進行Java String操作

[英]Java String operations by only using charAt (lastIndexOf, concatenate, substring, contain)

我想僅通過使用charAt在String中找到字符的lastIndexOf,但是我的代碼僅找到第一個匹配項。 我必須改變什么?

Scanner sc = new Scanner(System.in);
char operation = sc.nextLine().charAt(0);
if (operation == 'l' ) {
        System.out.print("Please enter a string: ");
        String enteredString = sc.next();
        System.out.print("Please enter a character: ");
        char char1 = sc.next().charAt(0);
        int index = 1;
        for (int i = 0; i < enteredString.length(); i++) {
            if (enteredString.charAt(i) == char1) {
                index = i;
                break;
            }
        }
        System.out.println("The index of character " + char1 + " in string " + enteredString + " is: " + index);

    }

我成功連接了兩個String:

Scanner sc = new Scanner(System.in);
char operation = sc.nextLine().charAt(0);
String c = "concatenation";
    if (operation == 'c' ) {
        System.out.println("Please enter the first string: ");
        String firstString = sc.next();
        System.out.println("Please enter the scond string: ");
        String secondString = sc.next();
        for (int i = 0; i < firstString.length(); i++) {
            char x = firstString.charAt(i);
            System.out.print(x);
        }
        for (int i = 0; i < secondString.length(); i++) {
            char y = secondString.charAt(i);
            System.out.print(y);
        }
    }

問題是,我實際上要打印此

System.out.println("The result of concatenating " + firstString + " and " + secondString + " is " + x + y);

但是我還沒有找到一種打印方法,因為x和y僅在for循環中定義,如果我嘗試打印它,它將被打印多次而不是一次。

當我通過charAt實現子字符串時,這也是一個問題:

if (operation == 's' ) {
        System.out.print("Please enter the string: ");
        String enteredString = sc.next();
        System.out.print("Please enter the first index: ");
        int index1 = sc.nextInt();
        System.out.print("Please enter the second index: ");
        int index2 = sc.nextInt();

        for (int i = index1; i < index2; i++) {
            char substring = enteredString.charAt(i);
            System.out.print(substring);

        }
}

我希望將其打印出來:

System.out.println("The resulting substring is: " + substring);

但是我也不知道如何實現這一目標。

非常簡單:尋找某個字符的最后一次出現時-只需向后移動字符串即可。

最后一個字符開始,然后在字符串內“向前”移動。 第一個匹配項是最后一個匹配項。

如果要保留從0到字符串末尾的循環:只需記住要查找的char的索引。 最初,該索引為-1,並且每次匹配時,您都將其更新為相應的索引。

正如@GhostCat所說
要查找字符的最后一個索引,只需替換代碼

int index = 1;
    for (int i = 0; i < enteredString.length(); i++) {
        if (enteredString.charAt(i) == char1) {
            index = i;
            break;
        }
    }
    System.out.println("The index of character " + char1 + " in string " + enteredString + " is: " + index);

有了這個

        int index = -1;
        for (int i = enteredString.length() - 1; i >= 0; i--) {
            if (enteredString.charAt(i) == char1) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            System.out.println("Character Not Found");
        } else {
            System.out.println("The index of character " + char1 + " in string " + enteredString + " is: " + index);
        }

對於子字符串操作,代碼應如下所示

System.out.print("The resulting substring is: ");
    for (int i = index1; i < index2; i++) {
        char substring = enteredString.charAt(i);
        System.out.print(substring);
    }
    System.out.println();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM