How to Remove rest String from particular index value.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-16-2023 09:41 AM
Hi All,
Please let me know solution for below query.
var str = "xx_yy_zz_dd_tt";
I want to remove last 2 values of this sprint. how can I remove?
In my requirement one thing is common that every time I need to remove last to values.
please explain in detail and also tell how I can split that and than how can I store.
I want to store only "xx_yy_zz" part.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2023 07:01 AM
You can use var finalString =str.substr(0,str.length-2); this will always remove the last 2 letters from your string.
Please mark it correct or helpful, if it resolve your query.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2023 10:15 AM
Hi @swechchha
You can use slice method as shown below
var str = "xx_yy_zz_dd_tt";
var editStr = str.slice(0, -2);
gs.print(editStr);
The slice() method is invoked on the str string and takes two parameters: 0 and -2.
- The first parameter, 0, represents the starting index from which the extraction should begin. In this case, it is 0, which means extraction starts from the beginning of the string.
- The second parameter, -2, represents the ending index up to which the extraction should occur. In this case, -2 means extraction will stop two characters before the end of the string.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2023 10:22 AM
Hi @swechchha ,
Hope you are doing great.
To remove the last two values from the string "xx_yy_zz_dd_tt" and store only the "xx_yy_zz" part, you can follow these steps:
- Split the string into an array using the underscore (_) as the delimiter.
- Remove the last two elements from the array.
- Join the remaining elements back into a string using the underscore (_) as the separator.
Here's an example implementation in Javascript:
var str = "xx_yy_zz_dd_tt";
var parts = str.split("_");
parts.splice(-2); // Remove the last two elements from the array
var result = parts.join("_"); // Join the remaining elements back into a string using the underscore (_) as the separator
console.log(result); // Output: "xx_yy_zz"
Regards,
Riya Verma