0

I've a string, "শাকসবজি কিনুন".

How can I cut off the " কিনুন" part of this string in java? Having issue over here because of Unicode.

I tried this :

String y1 = "শাকসবজি কিনুন";
        System.out.println(y1);
        y1 = y1.replace(" কিনুন", "");
        System.out.println(y1)

I got this

শাকসবজি কিন�ন
শাকসবজি
  • 3
    The issue appears to be that your terminal cannot display Unicode, rather than any issue with the trimming. – Andy Turner Feb 23 '20 at 07:50
  • I'm actually running this into a list and getting out of bound exception, while debugging through, in that case also, I'm getting this sort of characters, would be great if you could suggest something. –  Feb 23 '20 at 07:55
  • Try this https://eclipsesource.com/blogs/2013/02/21/pro-tip-unicode-characters-in-the-eclipse-console/ – Trishul Singh Choudhary Feb 23 '20 at 08:11
  • 1
    I run the code on my machine and get: শাকসবজি as result so it my be a issue with displaying Unicode. I am using Java 8 – Magdalena Fairfax Feb 23 '20 at 09:02
  • I'm also using java 8 –  Feb 23 '20 at 09:14
  • Voting to close, can not be reproduced. Is not related to the code, the code works fine. Needs more detail about the setup of the console. – Zabuzard Feb 23 '20 at 10:30

3 Answers3

1

Your terminal probably can't display Unicode.

Try piping your output into a file and opening it with a text editor.

If it works then there's nothing wrong with your code.

0

If you already know what parts to cut off you can use String.substring like so:

String y1 = "শাকসবজি কিনুন";
System.out.println(y1);
y1 = y1.substring(5)
System.out.println(y1)

To with this method, you can trim off a specified part of the string.

cc959
  • 103
  • 1
  • 12
0

As others have suggested, you may just need a font that will render Bengali characters. You don't state your environment, but one such font that is generally available on Windows is Arial Unicode MS.

Having set the font for the source code and the output to be Arial Unicode MS, your code works fine when I run your code in NetBeans:

Bengali font

Having an IDE makes using a specific font straightforward, but there may be additional steps needed depending on your operating system and specific requirements. For example, you may need to load a specific font and set the code page appropriately for command line output in Windows, but that is outside the scope of your question.

skomisa
  • 16,436
  • 7
  • 61
  • 102