java中连接字符串_Java中几种方式连接字符串的方法
字符串用于在Java中存储一系列字符,它们被视为对象。java.lang包的String类表示一个String。
您可以通过使用new关键字(如任何其他对象)或通过将值分配给文字(如任何其他原始数据类型)来创建String。String stringObject = new String("Hello how are you");
String stringLiteral = "Welcome to Tutorialspoint";
连接字符串
您可以通过以下方式在Java中连接字符串-
使用“ +”运算符:Java使用此运算符提供了一个串联运算符,您可以直接添加两个String文字
示例import java.util.Scanner;
public class StringExample {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first string: ");
String str1 = sc.next();
System.out.println("Enter the second string: ");
String str2 = sc.next();
//Concatenating the two Strings
String result = str1+str2;
System.out.println(result);
}
}
输出结果Enter the first string:
Krishna
Enter the second string:
Kasyap
KrishnaKasyap
Java
使用concat()方法 -String类的concat()方法接受一个String值,将其添加到当前String中并返回串联的值。
示例import java.util.Scanner;
public class StringExample {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first string: ");
String str1 = sc.next();
System.out.println("Enter the second string: ");
String str2 = sc.next();
//Concatenating the two Strings
String result = str1.concat(str2);
System.out.println(result);
}
}
输出结果Enter the first string:
Krishna
Enter the second string:
Kasyap
KrishnaKasyap
使用StringBuffer和StringBuilder类 -当需要修改时,StringBuffer和StringBuilder类可以用作String的替代类。
它们与String相似,但它们是可变的。这些提供了用于内容操纵的各种方法。这些类的append()方法接受一个String值,并将其添加到当前的StringBuilder对象中。
示例import java.util.Scanner;
public class StringExample {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first string: ");
String str1 = sc.next();
System.out.println("Enter the second string: ");
String str2 = sc.next();
StringBuilder sb = new StringBuilder(str1);
//Concatenating the two Strings
sb.append(str2);
System.out.println(sb);
}
}
输出结果Enter the first string:
Krishna
Enter the second string:
Kasyap
KrishnaKasyap
java中连接字符串_Java中几种方式连接字符串的方法相关推荐
- java反射效率对比_Java反射三种方式的效率对比
1 使用field long start = System.nanoTime(); Field[] fields = CallCount.class.getDeclaredFields(); for ...
- JAVA解决生产消费者_Java常用三种方式解决生产者消费者问题(详细)
package test; /** * Synchronized 版本解决生产者消费者 * wait() / notify()方法 */ import java.util.LinkedList; im ...
- python字符串换行的三种方式_Python 字符串换行的多种方式
第一种: x0 = '<?xml version="1.0"?>' \ ' ' \ ' Python' \ ' Ruby' \ ' ' 第二种: x1 = '<? ...
- C++中4种方式把字符串和数字连接起来
C++中4种方式把字符串和数字连接起来 帅东 以前老用Java里面的String类,用过的人都知道好舒服,连接字符串和数字只需要用一个 + 号就可以了.在这里真的想把C++中string类+号功能加强 ...
- Java中字符串拼接的几种方式(源码分析)
字符串拼接是我们在Java代码中比较经常要做的事情,就是把多个字符串拼接到一起. 我们都知道,String是Java中一个不可变的类,所以他一旦被实例化就无法被修改. 不可变类的实例一旦创建,其成员变 ...
- java new string 图_Java中String直接赋字符串和new String的一些问题
今天课堂测试做了几道String的练习题,做完直接心态爆炸...... 整理自下面两篇博客: 首先先来看看下面的代码: public classStringTest {public static vo ...
- c语言中按照“|”字符串截取,shell中取字符串子串的几种方式 截取substr
echo "123456789" | awk '{print substr($0,5,2)}' 截取 1)awk中函数substr substr(源字符串,开始索引,长度) 开 ...
- java 如何忽略异常_java中如何解决异常
原文 | dzone.com/articles/9-- 作者 | Thorben Janssen 翻译 | geekymv 无论你是初学者还是经验丰富的开发人员,对于你和你的团队来说,提高异常处理的能 ...
- 把一个数组的值存入二叉树中,然后利用前序、中序、后序3种方式进行遍历(完整代码以及运行结果)(Java)
把一个数组的值存入二叉树中,然后利用前序.中序.后序3种方式进行遍历(完整代码以及运行结果) 在最近的面试过程中,听说有小伙伴被面试官要求创建二叉树,然后对该二叉树进行遍历,感觉这一直以来都是一个大家 ...
- php连接虚拟机中mysql数据库吗,PHP连接MySQL数据库的三种方式
本篇文章给大家介绍一下PHP连接MysqL数据库的三种方式(MysqL.MysqLi.pdo),结合实例形式分析了PHP基于MysqL.MysqLi.pdo三种方式连接MysqL数据库的相关操作技巧与 ...
最新文章
- mysql如何做计划任务_简单的mysql 过程计划任务
- override render 方法
- EntityFramework进阶——数据编辑与维护
- Modbus教程| Modbus协议,ASCII和RTU帧,Modbus工作
- 「leetcode」77.组合【回溯算法】详解!
- http://acm.whu.edu.cn/land/problem/detail?problem_id=1464
- 32款 jQuery UI框架开源软件
- 一个vue的日历组件ele-calendar
- iphone 如何给cydia添加中文源和威锋源
- Linux T恤设计大赛 “飞企鹅”获奖
- 【优先队列】Toda 2
- 干货|如何写好项目进度报告
- 计算机文档字体替换,word2007进行字体替换的两种方法
- AVR单片机开发1——IO口的输入和输出
- 每周论文精读05-A2J:AnchortoJointRegressionNetwork for 3D ArticulatedPoseEstimation from a SingleDepthImage
- 【图解】本地IIS局域网内无法访问的问题
- Android 3年外包工面试笔记,有机会还是要去大厂学习提升
- UltraISO(软碟通)制作U盘启动安装CentOS 7
- 分布式搜索引擎 - 大总结
- 青少年学习机器人教育的收获
热门文章
- Python基础15_装饰器
- 精读《手写 SQL 编译器 - 回溯》 1
- 对话哈佛大学教授Lukin:量子计算将在我们有生之年普及!
- Phoenix报错(5) org.apache.hadoop.hive.ql.metadata.HiveException
- dropzone.js重写断点续传功能
- spark集成hbase与hive数据转换与代码练习
- 关于css3的:target伪类选择器
- 诺基亚接连巨亏:死守塞班难学摩托罗拉
- 微信小程序自定义组件(Toast)
- Treejs+EasyUI+Vuejs实操