Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace the first N dots of a string

Tags:

string

r

I hope to replace the first 14 dots of my.string with 14 zeroes when region = 2. All other dots should be kept the way they are.

df.1 = read.table(text = "
  city  county  state region                        my.string reg1 reg2
   1      1        1      1    123456789012345678901234567890   1    0
   1      2        1      1    ...................34567890098   1    0
   1      1        2      1    112233..............0099887766   1    0
   1      2        2      1    ..............2020202020202020   1    0
   1      1        1      2    ..............00..............   0    1
   1      2        1      2    ..............0987654321123456   0    1
   1      1        2      2    ..............9999988888777776   0    1
   1      2        2      2    ..................555555555555   0    1
", sep = "", header = TRUE, stringsAsFactors = FALSE)

df.1

I do not think this question has been asked here. Sorry if it has. Sorry also not to have spent more time looking for the solution. A quick Google search did not turn up an answer. I did ask a similar question here earlier: R: removing the last three dots from a string Thank you for any help.

I should clarify that I only want to remove 14 consecutive dots at the far left of the string. If a string begins with a number that is followed by 14 dots, then those 14 dots should remain the way they are.

Here is how my.string would look:

123456789012345678901234567890
...................34567890098
112233..............0099887766
..............2020202020202020
0000000000000000..............
000000000000000987654321123456
000000000000009999988888777776
00000000000000....555555555555
like image 622
Mark Miller Avatar asked Jan 28 '13 23:01

Mark Miller


People also ask

How do you replace a dot in a string?

To replace the dots in a string, you need to escape the dot (.) and replace using the replace() method.

How do you remove the dots from a string?

Use the String. replace() method to remove all dots from a string, e.g. const dotsRemoved = str. replace(/\./g, ''); . The replace() method will remove all dots from the string by replacing them with empty strings.

How do you replace all occurrences of a character in a string in Javascript?

To replace all occurrences of a substring in a string by a new one, you can use the replace() or replaceAll() method: replace() : turn the substring into a regular expression and use the g flag. replaceAll() method is more straight forward.

How do you change a dot with a slash in Java?

String a="\\*\\"; str=xpath. replaceAll("\\.", a);


1 Answers

Have you tried:

sub("^\\.{14}", "00000000000000", df.1$my.string )

For conditional replacement try:

> df.1[ df.1$region ==2, "mystring"] <- 
               sub("^\\.{14}", "00000000000000", df.1$my.string[ df.1$region==2] )
> df.1
  city county state region                      my.string reg1 reg2
1    1      1     1      1 123456789012345678901234567890    1    0
2    1      2     1      1 ...................34567890098    1    0
3    1      1     2      1 112233..............0099887766    1    0
4    1      2     2      1 ..............2020202020202020    1    0
5    1      1     1      2 ..............00..............    0    1
6    1      2     1      2 ..............0987654321123456    0    1
7    1      1     2      2 ..............9999988888777776    0    1
8    1      2     2      2 ..................555555555555    0    1
                        mystring
1                           <NA>
2                           <NA>
3                           <NA>
4                           <NA>
5 0000000000000000..............
6 000000000000000987654321123456
7 000000000000009999988888777776
8 00000000000000....555555555555
like image 173
IRTFM Avatar answered Oct 02 '22 11:10

IRTFM