向String添加元素的最简单方法是什么? [英] What is the easiest way to add elements to a String?

查看:486
本文介绍了向String添加元素的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,对于字符串book,我想将其设为boxxxxok。此外,如果我想将诸如book之类的元素替换为boxk,并且还将诸如book之类的元素删除为bok。最简单的方法是什么?

解决方案

这样做更容易,而不是在网上论坛上浪费时间。

开始阅读:

http://www.tutorialspoint.com/java/java_strings .htm [ ^ ]



https:// docs .oracle.com / javase / 7 / docs / api / java / lang / String.html [ ^ ]



你会在那里找到你需要的东西


有很多方法,问题是你可以为任何一组字符串解决问题。



例如:

    book .replace(  oo   ooxxxok); 



或使用子字符串切割

 string bo =   book .substring( 0  2 ); string ok =   book .substring( 2  2 



然后加在一起

 string s = bo.concat(  xxxx)。concat(ok); 


For instance, for a string "book", i want to make it "boxxxxok". Also if i want to replace one of the elements such as "book" to "boxk" and also delete an element such as "book" to "bok". What is the easiest method to do this?

解决方案

It's easier just to do it yourself rather than wasting time asking in an online forum.


Start reading:
http://www.tutorialspoint.com/java/java_strings.htm[^]
and
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html[^]

You will find what you need in there


There are many ways, the problem will be for you to work it out for any set of strings.

E.g:

"book".replace("oo","ooxxxok");


or use substring to cut

string bo="book".substring(0,2); string ok="book".substring(2,2)


then add together

string s = bo.concat("xxxx").concat(ok);


这篇关于向String添加元素的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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