Java 有没有一种方法可以使用循环拆分字符串,而不使用charAt(0)以外的任何函数?
我找不到以前发布的任何与此问题匹配的内容。我的任务是拆分一个字符串,然后返回拆分结果,而不使用您通常使用的任何函数。我可以用我知道长度的词来表达,但如果可能的话,我正试图找到一种方法来概括它 尽管指定了我们不允许执行的操作,但没有什么要说的了:除了length()、charAt()、equals()、equalsIgnoreCase()、toLowerCase()和toUpperCase()之外的字符串类中的任何其他方法。特别是,不允许使用substring()、split()、contains()、indexOf()或lastinexof() 我想我应该使用一个循环和一个if语句来找到单词(在本例中是一个电子邮件地址)应该拆分的位置(通过@符号),提取各个字符,然后将它们连接成一个字符串。我想知道的是,是否有一种方法可以推广它,这样我就可以提取charAt(I-n),而不是I-1、I-2、I-3等等。我觉得我走对了方向,但我有点卡住了Java 有没有一种方法可以使用循环拆分字符串,而不使用charAt(0)以外的任何函数?,java,string,loops,char,Java,String,Loops,Char,我找不到以前发布的任何与此问题匹配的内容。我的任务是拆分一个字符串,然后返回拆分结果,而不使用您通常使用的任何函数。我可以用我知道长度的词来表达,但如果可能的话,我正试图找到一种方法来概括它 尽管指定了我们不允许执行的操作,但没有什么要说的了:除了length()、charAt()、equals()、equalsIgnoreCase()、toLowerCase()和toUpperCase()之外的字符串类中的任何其他方法。特别是,不允许使用substring()、split()、contains
public static String getDomain (String s){
String domain = "domain";
int wordLength = s.length();
int i = 0;
for (i=0; i<= wordLength-1; i++) {
if (s.charAt(i) == '@') {
domain = (""+s.charAt(i-3)+s.charAt(i-2)+s.charAt(i-1));
System.out.println(domain);
return domain;
}
}
return domain;
公共静态字符串getDomain(字符串s){
字符串domain=“domain”;
int wordLength=s.length();
int i=0;
对于(i=0;i这可能是一种解决方案:
public static String getDomain (String s){
String domain = ""; // Consider using StringBuilder
int index = 0;
while(index < s.length()){
if(s.charAt(index) == '@'){
break;
}
index++;
}
for (int i = 0; i < index; i++){
domain += "" + s.charAt(i);
}
return domain;
}
公共静态字符串getDomain(字符串s){
字符串域=“”;/ /考虑使用String Bu建器
int指数=0;
而(索引
索引将递增,直到到达@符号。然后我们将连接每个字符,直到索引
另一种方式(只有一个循环):
公共静态字符串getDomain(字符串s){
字符串域=“”;/ /考虑使用String Bu建器
int指数=0;
而(索引
使用ArrayList进行拆分:
您可以使用存储结果的ArrayList
。数组列表本质上类似于数组,但它是动态的,因此您可以添加/删除其中的项,而无需声明其大小
下面,我对您的函数进行了一些修改。您可以传入自己的分隔符(拆分字符串的依据)以及要拆分的字符串,而不是总是按@
拆分
阅读下面的代码注释以更好地了解其工作原理:
public static ArrayList<String> mySplit(String toSplit, char del) {
ArrayList<String> result = new ArrayList<String>(); // create an empty array list to store/hold the result
String currentWord = ""; // create a string which will hold the current word we are looking to add to the array
for(int i = 0; i < toSplit.length(); i++) { // loop through every letter in the toSplit word
char currentChar = toSplit.charAt(i); // get the current character
if(currentChar != del) { // if the current character doesn't equal the "splitting character" then add it to our currentWord variable
currentWord += currentChar; // add the current character to the end of our currentWord string
} else { // the current char is the "splitting character"
result.add(currentWord); // add the current word to the results array
currentWord = ""; // set the current word back to empty (so the next word can use it)
}
}
result.add(currentWord);
return result;
}
这将导致:
example
domain.com
注意:要使用ArrayList
您需要首先导入它:
import java.util.ArrayList
不带ArrayList的拆分:
如果无法使用ArrayList
s,则需要计算最终返回的数组的大小
为此,您可以使用如下函数:
public static int countCharacter(String toCount, char character) {
int count = 0;
for(int i = 0; i < toCount.length(); i++) {
if(toCount.charAt(i) == character) count++;
}
return count;
}
然后,您可以在main
中获得结果数组(拆分数组),如下所示:
public static void main(String[ ] args) {
String[] splitDomain = mySplit("example@domain.com@", '@');
// Print the result from the function
System.out.println(splitDomain[0]);
System.out.println(splitDomain[1]);
}
或者我不明白你的问题,但为什么不直接使用substring()
:
/**
*使用java实现而不使用某些方法,例如:indexOf/substring/xxx
*允许:长度、ToCharray、StringBuffer
*禁止:索引、子字符串
*/
公共静态数组列表mySplit(字符串toSplit,字符串del){
ArrayList结果=新建ArrayList();
char[]target=del.toCharArray();
字符串currentWord=“”;
对于(int i=0;i
您的最终目标是基本上重新创建拆分
功能吗?如果我输入“example@domain.com
然后您得到一个数组作为[“example”,“domain.com”]的输出
?基本上,是的。这里有效率方面的考虑吗?如果看起来不错的话,那就没什么了。所以只有String类可以使用?为什么不同时使用这两个类呢?电子邮件中的Lol dmain在@
签名之后,用户名在前面。:)是的,你说得对。我只是想保留变量名以避免混淆,因为我只对一个单词使用这个方法,完全忽略数组而只使用currentWord+=currentChar(感觉不太可能)行吗?我以前从未使用过数组,这项作业只是对字符串的一种介绍,所以我不太知道有什么可能。我已经尝试过了,但坦率地说,我现在太累了。无论如何,谢谢你。@AlexanderElg是的,不使用数组也是可能的,但是你需要清楚地定义函数的输出是什么应该是。例如,如果您输入“example@domain.com“输出是什么?如果它只是一个字符串,就像你提到的那样,你希望它看起来怎么样?例如:像这样:“example,domain”
或这样:“example”
(只需返回@
之前的内容)或只返回@
之后的内容(即:“domain.com”)。根据您想要输出的内容,函数将变得更简单/复杂,因此清楚地概述您想要它返回的内容(输出)非常重要。我已经解决了。感谢您提供了if(currenChar!=del)。这正是所需要的
public static int countCharacter(String toCount, char character) {
int count = 0;
for(int i = 0; i < toCount.length(); i++) {
if(toCount.charAt(i) == character) count++;
}
return count;
}
public static String[] mySplit(String toSplit, char del) {
String[] result = new String[countCharacter(toSplit, del)+1]; // create an empty array with the amount of empty slots required to fit each splitted word.
String currentWord = "";
int indexToAdd = 0;
for(int i = 0; i < toSplit.length(); i++) { // loop through every letter in the toSplit word
char currentChar = toSplit.charAt(i); // get the current character
if(currentChar != del) { // if the current character doesn't equal the "splitting character" then add it to our currentWord variable
currentWord += currentChar; // add the current character to the end of our currentWord string
} else { // the current char is the "splitting character"
result[indexToAdd] = currentWord; // add the current word to the results array
currentWord = ""; // set the current word back to empty (so the next word can use it)
indexToAdd++; // increment to the next index
}
}
result[indexToAdd] = currentWord;
return result;
}
public static void main(String[ ] args) {
String[] splitDomain = mySplit("example@domain.com@", '@');
// Print the result from the function
System.out.println(splitDomain[0]);
System.out.println(splitDomain[1]);
}
public static String getDomain(String email) {
for (int i = 0, length = email.length(); i < length; i++) {
if (email.charAt(i) == '@') {
return email.substring(i + 1);
}
}
return "";
}
public static String getDomain(String email) {
char[] chars = email.toCharArray();
for (int i = 0, length = chars.length; i < length; i++) {
if (chars[i] == '@') {
return new String(chars, i + 1, length - i - 1);
}
}
return "";
}
/**
* Implemention using java without some of methods, eg: indexOf/substring/xxx
* allowed: length, toCharArray, StringBuffer
* forbid: indexOf、substring
*/
public static ArrayList<String> mySplit(String toSplit, String del) {
ArrayList<String> result = new ArrayList<String>();
char[] target = del.toCharArray();
String currentWord = "";
for(int i = 0; i < toSplit.length(); i++) {
int x = i, y = 0;
String temp = "";
while (y < target.length && target[y] == toSplit.charAt(x)){
temp += toSplit.charAt(x);
x ++;
y ++;
}
if (x == i){
temp += toSplit.charAt(x);
}
if (y < target.length ){
// 丢失的要找补回来
currentWord += temp;
}
else if(y == target.length){
i = i + y - 1;
result.add(currentWord);
currentWord = "";
}
else{
// 匹配成功
result.add(currentWord);
currentWord = "";
}
}
result.add(currentWord);
return result;
}