Delete Everything After Part of a String

Delete everything after part of a string

For example, you could do:

String result = input.split("-")[0];

or

String result = input.substring(0, input.indexOf("-"));

(and add relevant error handling)

Remove part of string after .

You just need to escape the period:

a <- c("NM_020506.1","NM_020519.1","NM_001030297.2","NM_010281.2","NM_011419.3", "NM_053155.2")

gsub("\\..*","",a)
[1] "NM_020506" "NM_020519" "NM_001030297" "NM_010281" "NM_011419" "NM_053155"

Deleting content of every string after first empty space

Assuming that there is no space in the beginning of the string.
Follow these steps-

  1. Split the string at space. It will create an array.
  2. Get the first element of that array.

Hope this helps.

str = "Example string"
String[] _arr = str.split("\\s");
String word = _arr[0];

You need to consider multiple white spaces and space in the beginning before considering the above code.

I am not native to JAVA Programming but have an idea that it has split function for string.
And the reference you cited in the question is bit complex, while you can achieve the desired thing very easily.

P.S. In future if you make a mind to get two words or three, splitting method is better (assuming you have already dealt with multiple white-spaces) else substring is better.

Remove everything after a certain character

You can also use the split() function. This seems to be the easiest one that comes to my mind :).

url.split('?')[0]

jsFiddle Demo

One advantage is this method will work even if there is no ? in the string - it will return the whole string.

Delete everything after 4th period in string

Use rsplit() for the cleanest or most simple route:

s = "192.168.0.50.XXXX"
s = s.rsplit('.',1)[0]

Output:

192.168.0.50

rsplit()

Returns a list of strings after breaking the given string from right
side by the specified separator.

how to remove all the string after a word in javascript?

It can be done the following way :

var input='bla\n\nbla\n\nShare something ...\n\nbla bla bla\n<image tag></img>bla\n<image tag></img>';

var output = input.split('Share something')[0]+'Share something';

console.log(output);

How to delete everything after matching string in R?

You can just do

gsub("SsHV2L.+$", "SsHV2L", test)

Here you grab the "SsHV2L" where there is something after it and then just replace all of that with only "SsHV2L"



Related Topics



Leave a reply



Submit