英文:
Replace or Rename String in Between two tags in a line and return as string in java
问题
以下是翻译后的字符串:
Name : Abul Bashar Fakir , cl : Knit Fabrics Manufacturer , In : 16 June 2003 , Hpc : President , Hpd : Assistant Governor , Bd : April 6 , Bg : B + , Wd : September 27 , Sp : Hossneara Begum , SBd : March 8 , SBg : 0+ , Ma : 98 North Chasara Narayanganj - 1400 , Tel : ( O ) 01617 008 519 ( R ) 763 2407 , Mob : 01715 393 127 01824 554 123 , Email : basharfokir66@gmail.com
英文:
I am trying to replace or rename a string/tags/keys in-between string in java.
Before jump into my string please notice that there should Bd
and Bg
and counts two times. Rename tags for the spouse section. its can remove duplicate and all key/tag is unique
My String is
Name : Abul Bashar Fakir , cl : Knit Fabrics Manufacturer , In : 16 June 2003 , Hpc : President , Hpd : Assistant Governor , Bd : April 6 , Bg : B + , Wd : September 27 , Sp : Hossneara Begum , Bd : March 8 , Bg : 0+ , Ma : 98 North Chasara Narayanganj - 1400 , Tel : ( O ) 01617 008 519 ( R ) 763 2407 , Mob : 01715 393 127 01824 554 123 , Email : basharfokir66@gmail.com
Please notice that Here Bd
and Bg
count 2 times. one is for the main person and the other is for his/her spouse and Tag is Sp
. I want to rename all Same tags for Sp with S character append. Example: Bd
and Bg
To SBd
, SBg
In-between Sp
tag and Ma
tag. it will create a unique tag for husband and spouse.
My Targeted String
Name : Abul Bashar Fakir , cl : Knit Fabrics Manufacturer , In : 16 June 2003 , Hpc : President , Hpd : Assistant Governor , Bd : April 6 , Bg : B + , Wd : September 27 , Sp : Hossneara Begum , SBd : March 8 , SBg : 0+ , Ma : 98 North Chasara Narayanganj - 1400 , Tel : ( O ) 01617 008 519 ( R ) 763 2407 , Mob : 01715 393 127 01824 554 123 , Email : basharfokir66@gmail.com
> Note: It must return as a String.
答案1
得分: 0
你可以使用String类的replaceAll方法。myString.replaceAll("bd", "Sbd")
英文:
You can use replace all from the String class. myString.replaceAll("bd", "Sbd")
答案2
得分: 0
只替换第一个'Bd'为'HBd'。
英文:
If you need to create a unique tag for husband and spouse then
Use Java String replaceFirst() method and rename husband tags with a specific name.
String str = "Name : Abul Bashar Fakir , cl : Knit Fabrics Manufacturer , In : 16 June 2003 , Hpc : President , Hpd : Assistant Governor , Bd : April 6 , Bg : B + , Wd : September 27 , Sp : Hossneara Begum , Bd : March 8 , Bg : 0+ , Ma : 98 North Chasara Narayanganj - 1400 , Tel : ( O ) 01617 008 519 ( R ) 763 2407 , Mob : 01715 393 127 01824 554 123 , Email : basharfokir66@gmail.com
";
//Only Replace first 'Bd' with 'HBd'
String str1 = str.replaceFirst("Bd", "HBd");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论