Remove Everything After Space in String

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 all string after a space

In both the cases, just split on " " and take the element on index 0. The below is for javascript

alert(text.split(" ")[0]); // Javascript

And this is for c#

Console.WriteLine(text.Split(' ')[0]);

Remove everything after space in string

strsplit("my string is sad"," ")[[1]][1]

Remove everything in a string after a second space if there is one

You need this, removes all characters after second space

String s = "Thundering Blow I";
int k = s.indexOf(" ", s.indexOf(" ") + 1);
String res = s.substring(0,k);
System.out.println(res);

Remove everything after last space with stringr

We can use sub

df %>% 
mutate(name = sub("\\s+[^ ]+$", "", name))

Or the same pattern in str_replace

df %>% 
mutate(name = str_replace(name, "\\s[^ ]+$", ""))
# A tibble: 4 × 2
# name value
# <chr> <dbl>
#1 Jake Lake 10.0
#2 Bay May 5.0
#3 Drake Cake Jr. 9.1
#4 Sam Ram IR 1.0

The pattern indicates a space (\\s) followed by one or more non white space (otherwise it can \\S+) until the end of the string and replace it with blank "". In the OP's code, it was non-specific (.*).

Remove everything before the last space

Your gsub("\\s*","\\1",str) code replaces each occurrence of 0 or more whitespaces with a reference to the capturing group #1 value (which is an empty string since you have not specified any capturing group in the pattern).

You want to match up to the last whitespace:

sub(".*\\s", "", str)

If you do not want to get a blank result in case your string has trailing whitespace, trim the string first:

sub(".*\\s", "", trimws(str))

Or, use a handy stri_extract_last_regex from stringi package with a simple \S+ pattern (matching 1 or more non-whitespace chars):

library(stringi)
stri_extract_last_regex(str, "\\S+")
# => [1] "vici"

Note that .* matches any 0+ chars as many as possible (since * is a greedy quantifier and . in a TRE pattern matches any char including line break chars), and grabs the whole string at first. Then, backtracking starts since the regex engine needs to match a whitespace with \s. Yielding character by character from the end of the string, the regex engine stumbles on the last whitespace and calls it a day returning the match that is removed afterwards.

See the R demo and a regex demo online:

str <- c("Veni vidi vici")
gsub(".*\\s", "", str)
## => [1] "vici"

Also, you may want to see how backtracking works in the regex debugger:

Sample Image

Those red arrows show backtracking steps.

How to remove everything after a certain character? (notepad++)

Do a find replace (replacing with nothing) this regex:

\|.*

See https://regexr.com/3rq9m for explanation.



Related Topics



Leave a reply



Submit