如何从字符串转换为数组

huangapple go评论71阅读模式
英文:

How to I go from a String to an Array

问题

import java.util.*;

public class scanner_LAB {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter a sentence with the word \"java\" in it: ");

        // input and end program
        System.out.println();
        System.out.println("Done - Press enter key to end program");
        String line = input.nextLine();
        System.out.println(line);

        // length
        int length = line.length();
        System.out.println("Your sentence length: " + length);

        // upper case and lower case
        String lowerCase = line.toLowerCase();
        System.out.println(lowerCase);

        String upperCase = line.toUpperCase();
        System.out.println(upperCase);
        System.out.println(lowerCase.indexOf("java"));

        // java to Java
        System.out.println(line.substring(0, line.toLowerCase().indexOf("java")) + "Java" + line.substring(line.toLowerCase().indexOf("java") + 4, line.length()));

        // java to JAVA
        System.out.println(line.substring(0, line.toLowerCase().indexOf("java")) + "JAVA" + line.substring(line.toLowerCase().indexOf("java") + 4, line.length()));

        // string to arrays
        String[] words = line.split(" ");
        System.out.println(Arrays.toString(words));
    }
}
英文:

I want to change my strings into array lists in my scanner program. I know that I have to use the .split method, but I am not sure how. I will use this example:

This is a good class with JAVA -> [This, is, a, good, class, with, Java]

This is what I have so far:

import java.util.*;
public class scanner_LAB {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a sentance with the word \"java\" in it: ");
// input and end program
System.out.println();
System.out.println("Done - Press enter key to end program");
String line = input.nextLine();
System.out.println(line);
// length
int length = line.length();
System.out.println("Your sent " + length);
// upper case and lower case 
String lowerCase = line.toLowerCase();
System.out.println(lowerCase);
String upperCase = line.toUpperCase();
System.out.println(upperCase);
System.out.println(lowerCase.indexOf("java"));
// java to Java
System.out.println(line.substring(0,line.toLowerCase().indexOf("java")) + "Java" + line.substring(line.toLowerCase().indexOf("java")+4, line.length()));
// java to JAVA
System.out.println(line.substring(0,line.toLowerCase().indexOf("java")) + "JAVA" + line.substring(line.toLowerCase().indexOf("java")+4, line.length()));
// string to arrays
}
}
</details>
# 答案1
**得分**: 1
你可以使用字符串的分割函数将字符串分割成数组。
String[] strArray = line.split(" ");
<details>
<summary>英文:</summary>
You can use the split function of string to split the string into array.
String[] strArray = line.split(&quot; &quot;);
</details>
# 答案2
**得分**: 0
```java
回答关于使用split方法的问题,以下是一般使用方法:
// 将字符串拆分为数组
String[] words = line.split(" ");
for (String word : words)
System.out.println(word);
我运行了你的代码,这对你的预期目的有效(我只是注释掉了与这个问题无关的一些代码)
import java.util.*;
public class scanner_LAB {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入包含单词“java”的句子:");
// 输入并结束程序
//System.out.println();
//System.out.println("完成 - 按回车键结束程序");
String line = input.nextLine();
System.out.println(line);
// 长度
int length = line.length();
System.out.println("你的句子长度为 " + length);
// 转换为小写和大写
String lowerCase = line.toLowerCase();
System.out.println(lowerCase);
String upperCase = line.toUpperCase();
System.out.println(upperCase);
System.out.println(lowerCase.indexOf("java"));
// 将java替换为Java
//System.out.println(line.substring(0,line.toLowerCase().indexOf("java")) + "Java" + line.substring(line.toLowerCase().indexOf("java")+4, line.length()));
// 将java替换为JAVA
//System.out.println(line.substring(0,line.toLowerCase().indexOf("java")) + "JAVA" + line.substring(line.toLowerCase().indexOf("java")+4, line.length()));
// 字符串拆分为数组
String[] words = line.split(" ");
System.out.println(Arrays.asList(words));
}
}
英文:

To answer your question about using the split method, this is the general way to use it
// string to arrays
String[] words = line.split(" ");
for(String word: words)
System.out.println(word);

I ran your code and this works four your intended purpose (I've just commented out some of your code that wasn't related to this question)

import java.util.*;
public class scanner_LAB {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(&quot;Please enter a sentance with the word \&quot;java\&quot; in it: &quot;);
// input and end program
//System.out.println();
//System.out.println(&quot;Done - Press enter key to end program&quot;);
String line = input.nextLine();
System.out.println(line);
// length
int length = line.length();
System.out.println(&quot;Your sent &quot; + length);
// upper case and lower case 
String lowerCase = line.toLowerCase();
System.out.println(lowerCase);
String upperCase = line.toUpperCase();
System.out.println(upperCase);
System.out.println(lowerCase.indexOf(&quot;java&quot;));
// java to Java
//System.out.println(line.substring(0,line.toLowerCase().indexOf(&quot;java&quot;)) + &quot;Java&quot; + line.substring(line.toLowerCase().indexOf(&quot;java&quot;)+4, line.length()));
// java to JAVA
//System.out.println(line.substring(0,line.toLowerCase().indexOf(&quot;java&quot;)) + &quot;JAVA&quot; + line.substring(line.toLowerCase().indexOf(&quot;java&quot;)+4, line.length()));
// string to arrays
String[] words = line.split(&quot; &quot;);
System.out.println(Arrays.asList(words));
}

}

huangapple
  • 本文由 发表于 2020年10月19日 17:23:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64424512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定