Java 制作字符串 shorthand/No replace() 函数 allowed/static 方法
Java Making a string shorthand/No replace() function allowed/static method
我一直遇到某个代码问题,我必须创建一个接收字符串的方法并将 returns 字符串转换为 shorthand。我不被允许使用替换函数,我还不知道如何使用数组。
我必须按照这些标准来适应:
A.replace these four words: "and" with "&", "to" with "2", "you" with "u", and "for" with "4" whenever they appear as separate words within a sentence.
B.remove all vowels ('a', 'e', 'i', 'o', 'u'), whether lowercase or uppercase, unless the vowel appears by itself.
例如For I shall love you forever
变成4 I shll lv u frvr
这是我正在编写的代码,我还没有开始,因为我不知道如何解决这个问题。
public static String shortHand(String str){
/* strategy:
* take string length. do a for loop for each individual letter and then search for letters and, replace
* with &, look for to, replace with 2, replace you w/ u, and then take out any vowels.
*/
for(int i = str.length(); i < str.length(); i++){
str.toLowerCase();
}
return "";
}
如果我能找到一个使用辅助方法但不是强制性方法的解决方案,那将会很有帮助。
尝试使用 HashMap 来存储键值对。在您的问题中,将要替换的词存储为键,即 "and",值应为“&”。
创建一组元音来检查它是否是元音。
试试下面的程序。
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class Test {
private static final Map<String, Character> keyMap = new HashMap<String, Character>(); // store key and value
private static final Set<Character> vowelSet = new HashSet<Character>(); // store vowel in set
static {
keyMap.put("and", '&');
keyMap.put("to", '2');
keyMap.put("you", 'u');
keyMap.put("for", '4');
vowelSet.add('a');
vowelSet.add('e');
vowelSet.add('i');
vowelSet.add('o');
vowelSet.add('u');
}
public static void main(String[] args) {
System.out.println(shortHand("For I shall love you forever"));
}
public static String shortHand(String str) {
StringBuffer result = new StringBuffer();
String[] strArr = str.split(" "); //seperate string by the space
for (int i = 0; i < strArr.length; i++) {
String temp = strArr[i].toLowerCase();
if (keyMap.containsKey(temp)) {//check in map eg. does it contains in the map then replace it with shorthand word.
result.append(keyMap.get(temp));
result.append(" ");
} else {
if (temp.length() == 1 && vowelSet.contains(temp.charAt(0))) {//if it is only a vowel
result.append(temp);
result.append(" ");
} else {
for (int j = 0; j < temp.length(); j++) {//check in every character if its vowel or not
if (!vowelSet.contains(temp.charAt(j))) {
result.append(temp.charAt(j));
}
}
result.append(" ");
}
}
}
return result.toString();
}
}
我一直遇到某个代码问题,我必须创建一个接收字符串的方法并将 returns 字符串转换为 shorthand。我不被允许使用替换函数,我还不知道如何使用数组。
我必须按照这些标准来适应:
A.replace these four words: "and" with "&", "to" with "2", "you" with "u", and "for" with "4" whenever they appear as separate words within a sentence.
B.remove all vowels ('a', 'e', 'i', 'o', 'u'), whether lowercase or uppercase, unless the vowel appears by itself.
例如For I shall love you forever
变成4 I shll lv u frvr
这是我正在编写的代码,我还没有开始,因为我不知道如何解决这个问题。
public static String shortHand(String str){
/* strategy:
* take string length. do a for loop for each individual letter and then search for letters and, replace
* with &, look for to, replace with 2, replace you w/ u, and then take out any vowels.
*/
for(int i = str.length(); i < str.length(); i++){
str.toLowerCase();
}
return "";
}
如果我能找到一个使用辅助方法但不是强制性方法的解决方案,那将会很有帮助。
尝试使用 HashMap 来存储键值对。在您的问题中,将要替换的词存储为键,即 "and",值应为“&”。
创建一组元音来检查它是否是元音。 试试下面的程序。
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class Test {
private static final Map<String, Character> keyMap = new HashMap<String, Character>(); // store key and value
private static final Set<Character> vowelSet = new HashSet<Character>(); // store vowel in set
static {
keyMap.put("and", '&');
keyMap.put("to", '2');
keyMap.put("you", 'u');
keyMap.put("for", '4');
vowelSet.add('a');
vowelSet.add('e');
vowelSet.add('i');
vowelSet.add('o');
vowelSet.add('u');
}
public static void main(String[] args) {
System.out.println(shortHand("For I shall love you forever"));
}
public static String shortHand(String str) {
StringBuffer result = new StringBuffer();
String[] strArr = str.split(" "); //seperate string by the space
for (int i = 0; i < strArr.length; i++) {
String temp = strArr[i].toLowerCase();
if (keyMap.containsKey(temp)) {//check in map eg. does it contains in the map then replace it with shorthand word.
result.append(keyMap.get(temp));
result.append(" ");
} else {
if (temp.length() == 1 && vowelSet.contains(temp.charAt(0))) {//if it is only a vowel
result.append(temp);
result.append(" ");
} else {
for (int j = 0; j < temp.length(); j++) {//check in every character if its vowel or not
if (!vowelSet.contains(temp.charAt(j))) {
result.append(temp.charAt(j));
}
}
result.append(" ");
}
}
}
return result.toString();
}
}