user10767069
user10767069

Reputation:

Trimming or cutting a a part of a string in java

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

শাকসবজি কিন�ন
শাকসবজি

Upvotes: 0

Views: 112

Answers (3)

skomisa
skomisa

Reputation: 17363

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.

Upvotes: 0

cc959
cc959

Reputation: 113

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.

Upvotes: 0

howDidMyCodeWork
howDidMyCodeWork

Reputation: 391

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.

Upvotes: 1

Related Questions