Replacement Array是一个元素数组。
格式字符串包含转换说明符%s,后跟可选的索引说明符[1],后跟可选的长度说明符:3。
格式字符串:%s [1]:3
%s is conversion specifier - mark the starting of conversion string
[1] is index specifier - corresponding index of Replacement Array
:3 is length specifier - number of characters to be taken from string
替换工作如下: 例如:
Replacement Array: Smart site India comparison Best
Format String: %sprix is the %s[4] online %s[3]:6 shopping %s:9 in %s
Output: Smartprix is the Best online compar shopping site in India.
If no index specifier is present with conversion specifier %s, Index 0, 1, 2, 3... are assigned to all in sequence from left to right.
In above example %sprix is %s[0]prix; %s:9 is %s[1]:9 and so on.
The specifier string is replaced by element of Replacement Array denoted by index specifier. If no element is found the specifier is not replaced.
In above example %s[4] is replaced by element at 4th index of the Replacement Array "Best". %sprix is replaced by Smartprix and so on.
%s[3]:6 is replaced by 'first 6 characters' of the element at 3rd index of the Replacement Array, i.e., "compar".
If the 'length specifier' is not present or is greater than length of the element then use whole string.
%s:9 is replaced by site, %s[4] is replaced by Best.
For any error case, specifier is not replaced.
输入:
输入中将有2行。
1st line contains space separated elements of Replacement Array
2nd line is Format String
输出:
Formatted String
示例1:
输入:
智能网站印度比较最佳 %sprix是%s [4]在线%s [3]:6%s%:9%s。
输出:
Smartprix是印度最好的在线比赛购物网站。
示例2:
输入:
印度繁荣启动中心 %s%s [是] a%唱%s:5%s:5%s [4]。 %s [6]是:%s [-1]。
输出:
印度%s是一个蓬勃发展的创业中心。 %s [6]是:%s [-1]。
答案 0 :(得分:1)
希望这适用于所有测试用例
package com.test.formatStrings;
import java.util.ArrayList;
import java.util.Scanner;
public class FormatStrings {
public static void main(String args[])throws Exception
{
Scanner sc= new Scanner(System.in);
String input_elements;
String replacement;
StringBuilder input_string=new StringBuilder();
//String output;
int lastindex1=0, count=0, lastindex2=0;
ArrayList<String> arrayElements= new ArrayList<String>();
System.out.println("Enter the array of elements");
input_elements= sc.nextLine();
System.out.println(input_elements);
for(String str: input_elements.split(" "))
{
arrayElements.add(str);
}
System.out.println("Enter the input String");
input_string.append(sc.nextLine());
int length=input_string.length();
for(int i=0;i<length;i++)
{
if(input_string.charAt(i)=='%')
{
int first_index=i;
if(input_string.charAt(i+1)=='s')
{
if(i+2<input_string.length() && input_string.charAt(i+2)=='[')
{
if(Character.isLetter(input_string.charAt(i+3))!=true && Character.getNumericValue(input_string.charAt(i+3))>-1)
{
int ArrayList_firstIndex=Character.getNumericValue(input_string.charAt(i+3));
if(input_string.charAt(i+4)==']')
{
if(input_string.charAt(i+5)==':')
{
if(Character.isLetter(input_string.charAt(i+6))!=true && Character.getNumericValue(input_string.charAt(i+6))>-1)
{
int ArrayList_lastIndex=Character.getNumericValue(input_string.charAt(i+6));
replacement=arrayElements.get(ArrayList_firstIndex).substring(0, ArrayList_lastIndex);
int last_index=i+6;
input_string.replace(first_index,last_index+1 , replacement);
length=input_string.length();
i=0;
}
}
else
{
if(ArrayList_firstIndex>arrayElements.size())
{
continue;
}
else
{
replacement=arrayElements.get(ArrayList_firstIndex);
input_string.replace(first_index, first_index+5, replacement);
length=input_string.length();
i=0;
}
}
}
}
}
else if(i+2<input_string.length() && input_string.charAt(i+2)==':')
{
if(Character.isLetter(input_string.charAt(i+3))!=true && Character.getNumericValue(input_string.charAt(i+3))>-1)
{
int ArrayList_lastIndex=Character.getNumericValue(input_string.charAt(i+3));
if(ArrayList_lastIndex>arrayElements.get(count).length())
{
replacement=arrayElements.get(count);
}
else
{
replacement=arrayElements.get(count).substring(0, ArrayList_lastIndex);
}
count++;
input_string.replace(first_index,first_index+4 , replacement);
length=input_string.length();
i=0;
}
}
else
{
replacement=arrayElements.get(count);
count++;
input_string.replace(first_index,first_index+2,replacement);
length=input_string.length();
i=0;
}
}
else
{
continue;
}
}
else
{
continue;
}
}
System.out.println(input_string);
sc.close();
}
}