文字列を「N」の等しい部分に分割するJavaプログラム
この記事では、文字列を「N」個の等しい部分に分割する方法を理解します。文字列は、1つ以上の文字を含み、二重引用符(“”)で囲まれたデータ型です。
以下は同じのデモンストレーションです-
入力がであると仮定します −
Input string: Java Program is fun!
必要な出力は −
The length of the string is: 20 4 equal parts of given string are Java Progr am is fun!
アルゴリズム
Step 1 - START Step 2 - Declare a string namely input_string, two integers namely string_length and N. Step 3 - Define the values. Step 4 - Initiatize a temporary variable to 0. Step 5 - Compute the parts in the string by dividing the length of the string by ‘N’. Step 6 - If the string is not divisible by N, display a relevant message. Given that the string length is divisible by N, iterate through the string. Step 7 - Fetch the substring within the range of string length and N in every iteration. Assign this value to a variable. Step 8 - Increment the temporary variable after every iteration. Step 9 - Display the N parts of the string on the console by iterating over the split parts of the string. Step 10 - Stop
例1
ここでは、「main」関数の下ですべての操作をバインドします。
public class Demo {
public static void main(String[] args) {
String input_string = "Java Program is fun!";
System.out.println("The string is defined as: " +input_string);
int string_length = input_string.length();
System.out.println("The length of the string is: " +string_length);
int N = 4;
int temp = 0, string_parts = string_length/N;
String[] equalStr = new String [N];
if(string_length % N != 0) {
System.out.println("The string cannot be divided int "+ N +" parts.");
} else {
for(int i = 0; i < string_length; i = i+string_parts) {
String part = input_string.substring(i, i+string_parts);
equalStr[temp] = part;
temp++;
}
System.out.println(N + " equal parts of given string are ");
for(int i = 0; i < equalStr.length; i++) {
System.out.println(equalStr[i]);
}
}
}
} 出力
The string is defined as: Java Program is fun! The length of the string is: 20 4 equal parts of given string are Java Progr am is fun!
例2
ここでは、操作をオブジェクト指向プログラミングを示す関数にカプセル化します。
public class Demo {
static void divide_string(String input_string, int N){
int string_length = input_string.length();
System.out.println("The length of the string is: " +string_length);
int temp = 0, string_parts = string_length/N;
String[] equalStr = new String [N];
if(string_length % N != 0) {
System.out.println("The string cannot be divided int "+ N +" parts.");
} else {
for(int i = 0; i < string_length; i = i+string_parts) {
String part = input_string.substring(i, i+string_parts);
equalStr[temp] = part;
temp++;
}
System.out.println(N + " equal parts of given string are ");
for(int i = 0; i < equalStr.length; i++) {
System.out.println(equalStr[i]);
}
}
}
public static void main(String[] args) {
String input_string = "Java Program is fun!";
System.out.println("The string is defined as: " +input_string);
int N = 4;
divide_string(input_string, N);
}
} 出力
The string is defined as: Java Program is fun! The length of the string is: 20 4 equal parts of given string are Java Progr am is fun!
-
括弧(または、)に一致するJava正規表現プログラム。
次の正規表現は、括弧付きの文字列を受け入れます- "^.*[\\(\\)].*$"; ^は文の先頭に一致します。 。*0個以上の(任意の)文字に一致します。 [\\(\\)]一致する括弧。 $は文の終わりを示します。 例1 import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class SampleTest { public static void main( String arg
-
文字列内の母音をカウントするJavaプログラム
以下が私たちの文字列だとしましょう- String myStr = "Jamie"; 同じ変数の母音を計算するので、変数count=0に設定します。すべての文字をループして母音を数えます- for(char ch : myStr.toCharArray()) { ch = Character.toLowerCase(ch); if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u