如何仅在Java中打印字符串的特定部分? [英] How to print only specific parts of a string in java?

查看:174
本文介绍了如何仅在Java中打印字符串的特定部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,该程序使用字符串的特定部分来写字母。

I am writing a program that writes a letter using specific parts of a string.

到目前为止,这是我所拥有的(我只是一个初学者)

Here is what I have so far (I am only a beginner)

import java.util.Scanner;

public class AutoInsurance {

    public static void main (String[] args)
    {
        Scanner scan=new Scanner (System.in);
        System.out.print("Enter Name:");
        String Name;
        Name=scan.nextLine();
        System.out.print("Enter Street Address:");
        String Address;
        Address=scan.nextLine();
        System.out.print("Enter city, state, and zip code:");
        String Location;
        Location=scan.nextLine();
        System.out.println();
        System.out.println();
        System.out.println("Dear "+Name+",");
        System.out.println(" You have been selected to receive this offer of auto insurance from");
        System.out.println("Allspam Insurance Company! Drivers from "++" saved an average "); // I just want it to print the city here, between ++

        // I will finish coding once I figure this out, but I'm stumped
    }
}


推荐答案

您在这里能做的最好的事情就是拆分您的地址字符串,用逗号隔开,并从结果数组中获取第一个值。

The best you can do here is to split your Adress string, by commas, and grab the first value from the resulting array.

看看这个问题,以获取有关在Java中拆分字符串的更多详细信息。

Take a look at this question for more details on splitting a string in Java.

我建议在您的代码中执行以下操作:

I suggest the following in your code:

String[] AddressParts = Address.split(",");
String City = AddressParts[0];
System.out.println("Allspam Insurance Company! Drivers from "+City+" saved an average ");

干杯!

这篇关于如何仅在Java中打印字符串的特定部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆