在Java中替换或重命名两个标记之间的字符串并作为字符串返回。

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

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");

huangapple
  • 本文由 发表于 2020年7月22日 07:26:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63024528.html
匿名

发表评论

匿名网友

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

确定