別の文字列から取得したアルファベット順の文字で文字列を作成するにはどうすればよいですか?
私がこのようなものを持っているとしましょう
String theWord = "Hello World";
新しい文字列を次のように計算するにはどうすればよいですか」
デルロー
これは単語ですが、アルファベット順に文字ごとにソートされています。
前もって感謝します
別の文字列から取得したアルファベット順の文字で文字列を作成するにはどうすればよいですか?
私がこのようなものを持っているとしましょう
String theWord = "Hello World";
新しい文字列を次のように計算するにはどうすればよいですか」
デルロー
これは単語ですが、アルファベット順に文字ごとにソートされています。
前もって感謝します
char[] chars = theWord.toCharArray();
Arrays.sort(chars);
String newWord = new String(chars);
同意しました、私は解決策を盗みました。しかし明らかに、空白を取り除いてすべてを小文字にすることも重要です:
char[] array = theWord.replaceAll("\\s+", "").toLowerCase().toCharArray();
Arrays.sort(array);
System.out.println(new String(array));
上記のソリューションはいずれもロケール固有のものではないため、このソリューションを使用しました。効率的ではありませんが、非常にうまく機能します..
public static String localeSpecificStringSort(String str, Locale locale) {
String[] strArr = new String[str.length()];
for(int i=0;i<str.length();i++){
strArr[i] = str.substring(i,i+1);
}
Collator collator = Collator.getInstance(locale);
Arrays.sort(strArr, collator);
StringBuffer strBuf = new StringBuffer();
for (String string : strArr) {
strBuf.append(string);
}
return strBuf.toString();
}
char[] arr = new char[theWord.length()];
for(int i=0;i<theWord.length;i++)
{
arr[i]=theWord.charAt(i);
}
for(int i=0;i<theWord.length();i++)
for(int j=0;j<theWord.length();j++)
{
char temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
int size=theWord.length();
theWord="";
for(int i=0;i<size;i++)
{
theWord+=arr[i];
}
import java.util.Arrays;
import java.util.Scanner;
// re arrange alphabets in order
public class RearrangeAlphabets {
@SuppressWarnings("resource")
public static void main(String[] args) {
String theWord;
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to rearrange: \n");
theWord = in.nextLine();
int length = theWord.length();
System.out.println("Length of string: "+length);
char[] chars=theWord.toCharArray();
Arrays.sort(chars);
String newWord=new String(chars);
System.out.println("The Re-Arranged word : "+newWord);
}
}
char[] chars2 = b.toLowerCase().toCharArray();
Arrays.sort(chars1);
String Ns1 = new String(chars1);