简体   繁体   English

如何使字符串排序程序不区分大小写?

[英]How do I make string sorting program case insensitive?

I am making a program that will sort a String alphabetically and I want to make it case insensitive.我正在制作一个按字母顺序对字符串进行排序的程序,我想让它不区分大小写。 So If I input "aBc" I want it to output "aBc" and not "Bac".因此,如果我输入“aBc”,我希望它输入 output“aBc”而不是“Bac”。 We are not using the Array.sort method instead we are learning about the different algorithms.我们没有使用 Array.sort 方法,而是在学习不同的算法。 For this project, it specifically said that we should use the selection sort algorithm.对于这个项目,它特别说我们应该使用选择排序算法。 The Kbd class is something we use in my Java class, and it's basically like scanner but made shorter into one line. Kbd class 是我们在我的 Java class 中使用的东西,它基本上类似于扫描仪,但缩短为一行。

public class Sträng_uppgift{
public static void main(String[] args){

    String unsorted_string = Kbd.readString("Write a word!, I will sort it alphabetically!");

    char[] chArr = unsorted_string.toCharArray();

    char temp;

    for(int i = 0; i < chArr.length; i++){
        for(int j = i+1; j < chArr.length; j++){
            if(chArr[i] > chArr[j]){
                temp=chArr[i];
                chArr[i] = chArr[j];
                chArr[j] = temp;
            }
        }
        System.out.print(chArr[i]);
    }

}

} }

Make your comparison case insensitive.使您的比较不区分大小写。 Convert this转换这个

if(chArr[i] > chArr[j]){

to use the same case on both sides.在双方使用相同的情况。 For example,例如,

if (Character.toUpperCase(chArr[i]) > Character.toUpperCase(chArr[j])) {

or或者

if (Character.toLowerCase(chArr[i]) > Character.toLowerCase(chArr[j])) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM